如何比较两个不同的时间值 [英] How to compare two different time values

查看:198
本文介绍了如何比较两个不同的时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查系统时间是否处于特定值。如果时间是上午11:15或者我可以使用,如果时间晚于11:00。

I want to check if the system time is at specific value. Like if the time is 11:15 am or not or I can use if the time is later than 11:00 am.

我试图使用

   CTime CurrentTime = CTime::GetCurrentTime();

但无法找出如何比较。

感谢

推荐答案

使用额外的CTime物件( Here ):

Use a additional CTime object(Here):

CTime CurrentTime=CTime::GetCurrentTime();

if(CurrentTime==CTime(2013,11,1,20,15,00))
{
    // Do that...
    Sleep(0);
}

但是与时间跨度比较总是更安全:

But it is always safer to compare against a timespan:

if((CurrentTime>CTime(2013,11,1,20,15,00))&&(CurrentTime<CTime(2013,11,1,20,15,05)))
{
    // Do that...
    Sleep(0);
}

在这种情况下,我花了5秒的时间。通常你永远不会碰到确切的时间,所以第二个例子是更安全。

In that case I took a span of 5 seconds. Usually you will never hit the exact time, so the second example is safer.

至少,回答你的问题,如果你想知道什么时候它的过去11: 00

And at least, to answer your question, if you want to know when its past 11:00

if(CurrentTime>CTime(CurrentTime.GetYear(),CurrentTime.GetMonth(),CurrentTime.GetDay(),11,00,00))
{
   // Uhh yeah, past 11:00
}

这篇关于如何比较两个不同的时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆