Subtracting 2 times to find hours and minutes?
-
Sunday, April 15, 2012 2:50 PM
// Get Start Time
SYSTEMTIME stStart;
m_dtpCtrl[0].GetSystemTime(&stStart);
// Get End Time
SYSTEMTIME stEnd;
m_dtpCtrl[1].GetSystemTime(&stEnd);
// Get Start Day
SYSTEMTIME stStartDay;
m_dtpCtrl[2].GetSystemTime(&stStartDay);
//Get End Day
SYSTEMTIME stEndDay;
m_dtpCtrl[3].GetSystemTime(&stEndDay);
// Set Times up for Calculation
CTimeSpan tsStart(stStartDay.wDay, stStart.wHour, stStart.wMinute, stStart.wSecond);
CTimeSpan tsEnd(stEndDay.wDay, stEnd.wHour, stEnd.wMinute, stStart.wSecond);
// Perform Calulation of total time worked
CTimeSpan temp = tsEnd - tsStart;pretty much just wanting to subtract 2 SYSTEMTIME struct from each other to get the time difference between them in order to calculate how long i've worked for a certain day! from the above code after the calculations are performed i can call temp.GetTotalHours() and it will return the correct ammount of hours i worked for a particular day. For some reason when i call temp.GetTotalMinutes() or even temp.GetMinutes() it always returns 0!!!! which is wrong when i alter the minute value of the SYSTEMTIME structs. Does anyone have any idea what i'm doing wrong in the above calculation? like i said im just wanting to subtract 2 times in order to get the hours and minutes i worked for a particular day
**UPDATE**
the above calculation does work...it's something in my print statement. For some reason the 2nd variable in the format statement always returns 0??? any ideas?
strTime.Format(_T("%d %d"), temp.GetTotalMinutes(), temp.GetTotalHours());
- Edited by lochnesse Sunday, April 15, 2012 2:53 PM update
All Replies
-
Sunday, April 15, 2012 2:59 PM
On 15/04/2012 16:50, lochnesse wrote:
the above calculation does work...it's something in my print statement. For some reason the 2nd variable in the format statement always returns 0??? any ideas?
strTime.Format(_T("%d %d"), temp.GetTotalMinutes(), temp.GetTotalHours());GetTotalMinutes and GetTotalHours return LONGLONG, so according to this table:
http://msdn.microsoft.com/en-us/library/tcxf1dw6(v=vs.100).aspx
you should use %lld as format specifier.
Giovanni
- Proposed As Answer by Jesse JiangMicrosoft Contingent Staff, Moderator Wednesday, April 18, 2012 9:19 AM
- Marked As Answer by Jesse JiangMicrosoft Contingent Staff, Moderator Monday, April 23, 2012 11:21 AM

