Computing.Net > Forums > Programming > How to find yesterday's date in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to find yesterday's date in C

Reply to Message Icon

Name: Santhosh
Date: November 14, 2002 at 08:13:52 Pacific
OS: Windows NT
CPU/Ram: Intel P3, 128MB RAM
Comment:

I need to manipulate date in my c program. I'm using SYSTEMTIME to get the current date. How can i get the yesterday's date. I can subtract 1 to the day but for first day of the month it will not work out. Is there any workaround for this. Any help is appreciated.



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: November 14, 2002 at 10:33:19 Pacific
Reply:

I don't remember the exact function, but the lowest level time function returns the system time in seconds since some base date (usually Jan 1, 1970).

So if you use that function, then subtract NumberOfSecondsInADay, you'll have the current time minus one day. Then you'll need to pass that value into the higher functions.

I did a quick glance at man pages and it looks like:

time() returns the time in seconds (use "man 2 time" for docs)

ctime(), gmtime(), localtime() can use the seconds value as input to make it meaningful to you (man ctime)


0

Response Number 2
Name: Whisky Bob
Date: November 14, 2002 at 10:42:57 Pacific
Reply:

Well honestly ive never bothered about what
day it is :), but i suppose there are
ways to get around this.
I suppose that SYSTEMTIME also tells what
month it is, so you can make a list that
tells how many days a month has.
For example, if its 1 JUN,
yesterdays date is 31 MAY. If its 1 MAY,
yesterdays date is 30 APR. And ofcourse you
have to take into consideration that every
fourth year FEB only got 27 days (or what
ever, how should i know! ). Now as i sed
ive never needed to know what date there is
in my programs, so you will have to figure
some code out. Use your imagenation!!!


0

Response Number 3
Name: Guy
Date: November 14, 2002 at 15:41:58 Pacific
Reply:

Hi -

/* This works on 32 bit machines, perhaps not on a 64 bit one */
time_t tta, ttb;
tta = time(NULL); /* right now */
printf("%s", ctime(&tta));
ttb = tta - 24*60*60; /* minus seconds in a day */
printf("%s", ctime(&ttb));

HTH, Guy



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Which is faster in perfor... Vb easy question



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How to find yesterday's date in C

C/C++How to find gard drive speed in RPM www.computing.net/answers/programming/cchow-to-find-gard-drive-speed-in-rpm/1704.html

how to find length of image IN C www.computing.net/answers/programming/how-to-find-length-of-image-in-c/3495.html

making batchfile 4 yesterday's date www.computing.net/answers/programming/making-batchfile-4-yesterdays-date/16801.html