CTimeSpan得到日期差

 

要获取两个时间差,如两个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 here CTime 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值