Solved

Comparing date in C

Posted on 1998-04-21
9
228 Views
Last Modified: 2010-04-15
Is there any way to compare a date with the system's date? If so, how do i do it?

0
Comment
Question by:GreatOne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 30 total points
ID: 1251424
you can get the current date and time and compare it difftime
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1251425
use the time() function to get the current system time (time_t)
convert your date into time_t type (which function you use depends on which date/time format your date is in).  THen use difftime() to compare the two times to get a difference in seconds.

0
 

Author Comment

by:GreatOne
ID: 1251426
that works well for comparing times, but how does that help me compare dates? In my code, i only want it to do certain things depending on the date. I dont see how this will help.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1251427
A date IS a time.

Convert the date obeject from whatever date/time format you have into a time_t and then compare.

Alternatively, turn both the system date and your date into strings and do a string compare.

0
 
LVL 84

Expert Comment

by:ozo
ID: 1251428
What is the format of your date?
What kinds of comparisons do you need to do?
0
 

Author Comment

by:GreatOne
ID: 1251429
Ozo,
The way I get the date from the system is to use the following code:

time_t now;
now=time(NULL);

and then i display it by using ctime(&now). the output is

DDD MMM dd hh:mm:ss yyyy

is there a better way to get the time?
The idea is for my program to do certain things on certain dates. I stored the dates as strings in an array and want it to compare it to the current date. the problem with my method is the time is  included when i get the current system date. got any ideas?

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1251430
simply convert the time into string using ctime (as you have already done)

then blank out the hh:mm:ss characters.

you can then compare these date strings for equality.

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1251431
pr you can use localtime to convert your time_t into a struct tm and from there you can test, say, the tm_year and tm_yday values for a given day.

0
 

Author Comment

by:GreatOne
ID: 1251432
Thanks ronslow. using localtime made it very easy to accomplish my task.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C hashtable library 3 113
Windows 2012 R2 -- create 3.5TB GPT from unallocated space ? 1 91
maximize the sum of fractions 33 74
keep track of class structure 1 53
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question