0

I am trying to add days to a formatted date in C++, but without any success. The date is passed as a SYSTEMTIME type, and days to add in long type. In the following code example i am adding the days in a date converted to long, and this is wrong, i am using this just as an example.

long FormatDate(SYSTEMTIME* cStartTime, long daysToAdd)
{
    UCHAR szToday[16];

    sprintf((char*)szToday, "%04d%02d%02d", cStartTime->wYear, cStartTime->wMonth, (cStartTime->wDay)); 
    
    long finalDate = atol((char*)szToday) + daysToAdd // e.g. if szToday is "20210601" and daysToAdd is 10, then finalDate is 20210611
    
    return finalDate;
}

Thanks.

Pl4tinum
  • 123
  • 15

1 Answers1

0

After some search and debugging i am using the following code, and it's working. Note that hour, minute, second and millisecond from CustomDate must be set, otherwise it won't work. In this scenario i'm adding seconds, so it could be more generic. So when i need to convert to days i do this: daysToAdd * 24 * 60 * 60.

SYSTEMTIME AddSeconds(SYSTEMTIME s, INT64 seconds) {

FILETIME f;
SystemTimeToFileTime(&s, &f);

(INT64&)f += seconds * 10000000L;

FileTimeToSystemTime(&f, &s);

return s;

}

void Func() {

INT64 daysToAdd = 15;

SYSTEMTIME customDate;
customDate.wYear = 2021;
customDate.wMonth = 1;
customDate.wDay = 1;
customDate.wHour = 0;
customDate.wMinute = 0;
customDate.wSecond = 0;
customDate.wMilliseconds = 0;

INT64 secondsToAdd = daysToAdd * 24 * 60 * 60;

SYSTEMTIME finalDate = AddSeconds(customDate, secondsToAdd);

}

Pl4tinum
  • 123
  • 15