increase date?

using the [url="http://msdn.microsoft.com/en-us/library/dd757629%28v=vs.85%29.aspx"]TimeGetTime[/url] function, how can i add days or even months to the current time?
how can i add days or even months to the current time?

Using this function, unfortunately you can't:

MSDN wrote:
The return value wraps around to 0 every 2^32 milliseconds, which is about 49.71 days.

So, if the return value is 0, the most you can add to the return value is 49.71 days.

You should use the GetSystemTime or GetLocalTime function instead.
http://msdn.microsoft.com/en-us/library/ms724390(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms724950(v=vs.85).aspx

The value returned is a SYSTEMTIME struct, which has the year, month, day, dayofweek, and time (to the millisecond). You can manually add days/months/years to the struct or there may be some built-ins (haven't looked into it :).
thnks for info!
how would i increase the time by a day/month using any of those functions you mentioned?
how would i increase the time by a day/month using any of those functions you mentioned?

1. Manually

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
SYSTEMTIME st;

//there are differences between these two. Check out MSDN for more info :)
GetSystemTime(&st);
//GetLocalTime(&st);

//add 1 month
st.wMonth++;
if(st.wMonth == 13)
{
     st.wMonth = 1;
}

//add 1 day
WORD wMaxDay;

//30 days hath September, April, June, and November
if((st.wMonth == 9) || (st.wMonth == 4) || (st.wMonth == 6) || (st.wMonth == 11))
{
     wMaxDay = 30;
}
//except February alone ... ... the song breaks in code :P
else if(st.wMonth == 2)
{
     //which has but 28 days clear
     wMaxDay = 28;

     //and 29 in each leap year
     if((st.wYear % 4) == 0)
     {
          wMaxDay = 29;
     }
}
//all the rest have 31
else
{
     wMaxDay = 31;
}

if(st.wDay == wMaxDay)
{
     st.wDay = 1;

     //add 1 month
     st.wMonth++;
     if(st.wMonth == 13)
     {
          st.wMonth = 1;
     }
}
else
{
     st.wDay++;
}


2. Converting to a FILETIME structure and deal with its underlying LARGE_INTEGER representation (where adding 1 is adding 100 nanoseconds)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
SYSTEMTIME st;
FILETIME ft;

GetSystemTime(&st);
//GetLocalTime(&st);

SystemTimeToFileTime(&st, &ft);

ULARGE_INTEGER ftAsULargeInt;    
memcpy(&ftAsULargeInt, &ft, sizeof(ftAsULargeInt));

const double secondsPer100ns = 100.*1.e-9;
const double minutesPer100ns = secondsPer100ns*60;
const double hoursPer100ns = minutesPer100ns*60;
const double daysPer100ns = hoursPer100ns*24;

//add 5 days
ftAsULargeInt.QuadPart += (double) 5 / daysPer100ns;

//add 3 months (assume current month is July) (will have to do some manual calculations here to 
//actually only add three to the months component)
ftAsULargeInt.QuadPart += (double) (31 + 31 + 30) / daysPer100ns; //31 days in July and August, 30 in September

//add 1 year (this year is not a leap year) (again, will have to do some manual calculations here to actually
//only add one to the year component)
ftAsULargeInt.QuadPart += (double) 365 / daysPer100ns;

//convert back to file time
memcpy(&ft, &ftAsULargeInt, sizeof(ft));

//convert back to system time
FileTimeToSystemTime(&ft, &st);

//can now access the modified date!
WORD day = st.wDay;
WORD month = st.wMonth;
WORD year = st.wYear;
Last edited on
Topic archived. No new replies allowed.