在VC中计算两个日期时间中的时间差(分钟单位)

// 起始和结束日期时间关联的日期时间变量
        COleDateTime m_tStartDate;
        COleDateTime m_tStartTime;
        COleDateTime m_tEndDate;
        COleDateTime m_tEndTime;

        // 格式化后的起始和结束的日期时间变量
        COleDateTime  m_tStartDateTime;
        COleDateTime  m_tEndDateTime;

        void CXXXDlg::OnCalculate()
        {
                // TODO: Add your control notification handler code here
                UpdateData();

                // 格式化起始和结束的日期时间
                m_tStartDateTime.SetDateTime(
                                                m_tStartDate.GetYear(),
                                                m_tStartDate.GetMonth(),
                                                m_tStartDate.GetDay(),
                                                m_tStartTime.GetHour(),
                                                m_tStartTime.GetMinute(),
                                                m_tStartTime.GetSecond());

                m_tEndDateTime.SetDateTime(
                                                m_tEndDate.GetYear(),
                                                m_tEndDate.GetMonth(),
                                                m_tEndDate.GetDay(),
                                                m_tEndTime.GetHour(),
                                                m_tEndTime.GetMinute(),
                                                m_tEndTime.GetSecond());

                COleDateTimeSpan tMinute = m_tEndDateTime - m_tStartDateTime;  // 计算两个日期时间的差值
                int iMinute = (int)tMinute.GetTotalMinutes();
                CString strMinute = _T("");
                strMinute.Format("%d", iMinute);
                MessageBox(strMinute);
        }

 

获得当前时间:

SYSTEMTIME sysTm;
 ::GetLocalTime(&sysTm);

m_tStartDateTime.SetDateTime(sysTm.wYear,sysTm.wMonth,sysTm.wDay,sysTm.wHour,sysTm.wMinute,sysTm.wSecond);

posted on 2010-04-21 13:17  carekee  阅读(7535)  评论(0编辑  收藏  举报