Computing.Net > Forums > Programming > COleDateTime adding days

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.

COleDateTime adding days

Reply to Message Icon

Name: CRaig
Date: November 4, 2002 at 08:46:36 Pacific
OS: -
CPU/Ram: -
Comment:

Can anyone tell me what the best way to add days to an COleDateTime object is?



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff J
Date: November 4, 2002 at 20:45:44 Pacific
Reply:

There are several ways, depending on the data type you want to add. COleDateTime just encapsulates the DATE type, which is simply a double. The fractional part holds time, and the integral part holds days. Thus, you could simply add a day by doing something like this:


COleDateTime dt(2002, 11, 4, 0, 0, 0);
dt.m_dt += 1; //day becomes the 5th


The m_dt member is the double value itself, so you can add and subtract days from it just by using integers. Although it is the most efficient way, it is not the "official" way ;)

A more sophisticated way, and one which allows modifying the time parts too, is to use the overloaded += operator. It takes a COleDateTimeSpan object, which holds a range of days+time. The other overloaded operators (+,-,-=,=) also use that type. For example (extending the previous code):


COleDateTimeSpan span(1); //1 day

//or in days/hours/min/secs...
//COleDateTimeSpan span(1,0,0,0);

int iDay = dt.GetDay(); //currently 5

dt += span;

iDay = dt.GetDay(); //now 6


Cheers


0
Reply to Message Icon

Related Posts

See More


Why can't I use boolean d... VB apps and Freezing



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: COleDateTime adding days

Delete Files Older Then x-Days www.computing.net/answers/programming/delete-files-older-then-xdays/15229.html

Help creating class day www.computing.net/answers/programming/help-creating-class-day/12589.html

Deleting older than 7 day directories www.computing.net/answers/programming/deleting-older-than-7-day-directories/20125.html