DateAdd equivalent?
-
Is there a c++ equivalent of vbs's DateAdd where you can add or subtract any amount of hours, days, months etc. to a date? - thanks
If you are using MFC, see the
COleDateTime
class.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
If you are using MFC, see the
COleDateTime
class.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Is there a c++ equivalent of vbs's DateAdd where you can add or subtract any amount of hours, days, months etc. to a date? - thanks
See if it helps you ... int nDay; int nMonth; int nYear; struct tm *when; time_t now, result; time( &now ); when = localtime( &now );//conversion from UTC to system time if(when == NULL)//Error in the calculation of time return -1; when->tm_mday = when->tm_mday + nDays; result = mktime( when ); if(result == (time_t)-1)//size of time_t = 0;result=-1--->error in calculation of result { return -1; } else { nDay = when->tm_mday; nMonth = when->tm_mon+1; nYear = when->tm_year+1900; } impossible to understand
-
Ok. How are you obtaining the date value you want to manipulate?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Ok. How are you obtaining the date value you want to manipulate?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
So do you have a
SYSTEMTIME
or aFILETIME
object at this point?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
So do you have a
SYSTEMTIME
or aFILETIME
object at this point?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
SYSTEMTIME.
SYSTEMTIME stDateIWantToAddDaysOrMonthsTo;
FILETIME ft, ftLocal;
GetSystemTimeAsFileTime(&ft);
FileTimeToLocalFileTime(&ft, &ftLocal);
FileTimeToSystemTime(&ftLocal, &stDateIWantToAddDaysOrMonthsTo);Ok, so what problem(s) are you having with "add or subtract any amount of hours, days, months etc. to a date?"
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Ok, so what problem(s) are you having with "add or subtract any amount of hours, days, months etc. to a date?"
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
I don't know of any function that does this. I was looking for something like VBScript's DateAdd where I could do something like
DateAdd(Month, 24, &stMyDate)
and it would add 24 months to the date (2 years), taking into account leap years etc..nm_114 wrote:
I don't know of any function that does this.
Why would you need a function, when one statement will work:
stDateIWantToAddDaysOrMonthsTo.wYear += 2;
You could roll your own function to this I suppose. Take a stroll through the other date/time functions to see what is available to you.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
nm_114 wrote:
I don't know of any function that does this.
Why would you need a function, when one statement will work:
stDateIWantToAddDaysOrMonthsTo.wYear += 2;
You could roll your own function to this I suppose. Take a stroll through the other date/time functions to see what is available to you.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
DavidCrow wrote:
Why would you need a function, when one statement will work:
It was a bad example I guess. It needs to be arbitrary. Like
DateAdd(Month, 309, mydate)
,DateAdd(Day, -909, mydate)
etc. I'm not sure if I'm explaining it right.DavidCrow wrote:
Take a stroll through the other date/time functions to see what is available to you.
I did. I didn't see anything like DateAdd.
-
DavidCrow wrote:
Why would you need a function, when one statement will work:
It was a bad example I guess. It needs to be arbitrary. Like
DateAdd(Month, 309, mydate)
,DateAdd(Day, -909, mydate)
etc. I'm not sure if I'm explaining it right.DavidCrow wrote:
Take a stroll through the other date/time functions to see what is available to you.
I did. I didn't see anything like DateAdd.
If you convert the
SYSTEMTIME
structure to aFILETIME
structure, you can add/subtract such values.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
If you convert the
SYSTEMTIME
structure to aFILETIME
structure, you can add/subtract such values.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
:confused: How? I found an article at http://www.codeproject.com/datetime/winapi_datetime_ops.asp[^] but it doesn't work with months/years, and I have no idea if it works right with leap years etc.
nm_114 wrote:
How?
By using
SystemTimeToFileTime()
.nm_114 wrote:
I found an article at http://www.codeproject.com/datetime/winapi\_datetime\_ops.asp\[^\]
That article is exactly what you need.
nm_114 wrote:
but it doesn't work with months/years
That's because a month does not have a constant value (i.e., 28, 29, 30, or 31 days) like the other items do.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
nm_114 wrote:
How?
By using
SystemTimeToFileTime()
.nm_114 wrote:
I found an article at http://www.codeproject.com/datetime/winapi\_datetime\_ops.asp\[^\]
That article is exactly what you need.
nm_114 wrote:
but it doesn't work with months/years
That's because a month does not have a constant value (i.e., 28, 29, 30, or 31 days) like the other items do.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
DavidCrow wrote:
nm_114 wrote: How? By using SystemTimeToFileTime().
Well I knew that much! :-D I was asking how you do the adding/subtracting...
DavidCrow wrote:
That article is exactly what you need.
Well not really, as it doesn't handle months/years.
DavidCrow wrote:
That's because a month does not have a constant value (i.e., 28, 29, 30, or 31 days) like the other items do.
I knew that too! :-D But it's ok, I guess what I was looking for doesn't exist (a tested/bug-free exact replica of DateAdd() in C++). Oh well :sigh:. Thanks anyway.
-
DavidCrow wrote:
nm_114 wrote: How? By using SystemTimeToFileTime().
Well I knew that much! :-D I was asking how you do the adding/subtracting...
DavidCrow wrote:
That article is exactly what you need.
Well not really, as it doesn't handle months/years.
DavidCrow wrote:
That's because a month does not have a constant value (i.e., 28, 29, 30, or 31 days) like the other items do.
I knew that too! :-D But it's ok, I guess what I was looking for doesn't exist (a tested/bug-free exact replica of DateAdd() in C++). Oh well :sigh:. Thanks anyway.
nm_114 wrote:
Well not really, as it doesn't handle months/years.
I already showed you how to add months to the
SYSTEMTIME
object. You would add years in the same fashion. What you have to account for when adding months is to make sure to increment years accordingly. For example, if it's September and you want to add four months, you would use:wMonth = (wMonth + 4) % 12; // which would produce 0, for January
But you would then have to add one to the year.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
nm_114 wrote:
Well not really, as it doesn't handle months/years.
I already showed you how to add months to the
SYSTEMTIME
object. You would add years in the same fashion. What you have to account for when adding months is to make sure to increment years accordingly. For example, if it's September and you want to add four months, you would use:wMonth = (wMonth + 4) % 12; // which would produce 0, for January
But you would then have to add one to the year.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Yep. I guess I was hesitant to write my own because I don't know how to account for leap years, but I guess it's not critical. I'll probably use that other article and write something similar to your example for months/years. Thanks.
nm_114 wrote:
...I don't know how to account for leap years...
Are you kidding? Leap year algorithms/functions are plentiful here at CP, or even Googling for them.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb