Sign in to follow this  

Date Information

This topic is 2543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am programming in VSC++2010 environment.
I want to create a folder which will be named as yesterday's date e.g "2010-12-26-SUN" if to day is "2010-12-27-MON"
I am worry about
1/on Jan 1st each year, I have to get the above date format that can move back to Dec 31 of the previous year
2/on March 1st of a leap year, how do I get the date format to move back to Feb 28th

I can get get today's date using the following 2 lines

SYSTEMTIME localTime;
GetLocalTime(&localTime);

But I can't get yesterday date from GetLocalTime. Can anyone please advise me how to get yesterday's date please?

Thanks in advance.
Sorcerer

Share this post


Link to post
Share on other sites
I'm not sure with a windows SYSTEMTIME construct, but I think with C++ "tm" struct it is pretty simple.

Something like this:


#include <time.h>

time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );

timeinfo->tm_mday--;
mktime (timeinfo);



mktime I think looks only at the year, month, and monthday, and updates all the rest of the yearday/weekday/month/year info from that.

Even if you input a struct with Jan-01 2011, and subtract 1 from monthday, I think it figures out that (mday=-1,year=2011) is the same as december 31, 2010. It's pretty useful. I guess it should work for leap years too, but you should probably do a testcase to make sure.

Share this post


Link to post
Share on other sites
Thank you. I overlooked mktime() altogether!
Quick question. In running localtime (&rawtime), the timeinfo->tmyear = 110. I added 1900 to 110 to get the current year. Is this the correct procedure?
Thanks in advance.
Sorcerer

Share this post


Link to post
Share on other sites
Since you're using Windows, you could make use of SystemTimeToFileTime and FileTimeToSystemTime, and treat the FILETIME as an __int64, subtracting the necessary amount to go back by one day.

Share this post


Link to post
Share on other sites

This topic is 2543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this