CTimeSpan时间差

来源:互联网 发布:微信电子相册制作软件 编辑:程序博客网 时间:2024/06/16 22:51
要获取两个时间差,如两个CTime的时间差,可以使用MFC中的CTimeSpan类。
CTime time1 = CTime::GetCurrentTime(); CTime time2 = CTime::GetCurrentTime(); // 两个CTime相减得到CTimeSpan CTimeSpan timeSpan = time2 - time1; // 得到总的秒数 int nTSeconds = timeSpan.GetTotalSeconds();
     注意GetTotalSeconds与GetSeconds的区别:GetTotalSeconds返回总的秒数,GetSeconds返回总是小于60,如:如果时间经过了100秒, GetTotalSeconds返回100,而GetSeconds返回40,因为有60秒转为一分钟了,同时使用GetMinutes会返回1,即1分40秒。其它类似函数:
GetDays(); // 返回日数 GetHours(); // 返回小时数(-23至23之间) GetTotalHours(); // 返回总的小时数 GetMinutes(); // 返回分钟数(-59至59之间) GetTotalMinutes(); // 返回总的分钟数 GetSeconds(); // 返回秒数(-59至59之间) GetTotalSeconds(); // 返回总的秒数
例如:
CTimeSpan m_timespan(3,4,5,6); // 3天,4小时,5分,6秒

//参数说明

CTime tmToday; //今天的日期 CTime tmPreTwoDay; //前两天的日期 CTimeSpan tmspanTwoDay(2,0,0,0); //两天的时间距离

tmToday=CTime::GetCurrentTime();//获得当前日期 tmPreTwoDay=tmToday-tmspanTwoDay;

// TODO: Add extra validation hereCTime tmToday; //今天的日期 CTime tmPreTwoDay; //前两天的日期 CTimeSpan tmspanTwoDay(1,0,0,0); //两天的时间距离 //CTimeSpan m_timespan(3,4,5,6); // 3天,4小时,5分,6秒CString a;//获取前两天日期for(int i=1;i<3;i++){tmToday=CTime::GetCurrentTime();//获得当前日期 CTimeSpan tmspanTwoDay(i,0,0,0); //两天的时间距离

tmPreTwoDay=tmToday-tmspanTwoDay; a.Format("%02d %02d %02d",tmPreTwoDay.GetYear()%100,tmPreTwoDay.GetMonth(),tmPreTwoDay.GetDay());AfxMessageBox(a);}

0 0
原创粉丝点击