Don san
Don san

Reputation: 25

Subtract one second from given CTime

I am working on a VC++ project using MFC. There's a CTime object in my function that has a value of "10/11/2016 03:00:00".......something like that.

I want to subtract one second from the time part. So it will look like

"10/11/2016 02:59:59"

So for e.g., if the CTime value is "10/22/2016 07:45:50" then after subtraction by 1 second it should be "10/22/2016 07:45:49"

I tried,

CTime - 1, but this gives an error.

UPDATE:

I fixed this via,

time_t myTime= CTime.GetTime();
myTime= myTime - 1;

time_t gives the time in seconds and thus it's straight forward at this point.

Upvotes: 1

Views: 837

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37192

To add or subtract to or from a CTime you need to use a CTimeSpan object to indicate the duration you want to add or subtract.

For example, to subtract a second:

CTime cNewTime = cOldTime - CTimeSpan(0, 0, 0, 1);

Upvotes: 4

Related Questions