• C

Comparing date in C

Is there any way to compare a date with the system's date? If so, how do i do it?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you can get the current date and time and compare it difftime

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

GreatOneAuthor Commented:
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.
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

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.

What is the format of your date?
What kinds of comparisons do you need to do?
GreatOneAuthor Commented:
The way I get the date from the system is to use the following code:

time_t now;

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?

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.

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.

GreatOneAuthor Commented:
Thanks ronslow. using localtime made it very easy to accomplish my task.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.