使用C#检查时间是否存在两次(2 PM-3 Pm)之间 [英] Check whether The time exists between two times( 2PM-3 Pm) using C#

查看:67
本文介绍了使用C#检查时间是否存在两次(2 PM-3 Pm)之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想验证时间,输入的时间是否在两次之间使用C#



任何想法??



谢谢,

Priya

解决方案

  //  将所有内容转换为TimeSpan  
TimeSpan start = new TimeSpan( 22 0 0 );
TimeSpan结束= TimeSpan( 07 0 0 );
TimeSpan now = DateTime.Now.TimeOfDay;
// 查看start是否在结束之前
if (start < end)
return start < span class =code-keyword>< = now&&现在< = end;
// start is after after,所以反向比较
return !(end < = now&& now < ; =开始);





我认为它有用....


< blockquote>使用类型 System.DateTime ,它支持运算符:'<','< =','>','> ='和' =='。



-SA


Hi,

I want to validate the time, Whether the entered time is falls between the two times using C#

Any Thoughts??

Thanks,
Priya

解决方案

// convert everything to TimeSpan
TimeSpan start = new TimeSpan(22, 0, 0);
TimeSpan end = new TimeSpan(07, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
// see if start comes before end
if (start < end)
    return start <= now && now <= end;
// start is after end, so do the inverse comparison
return !(end <= now && now <= start);



I Think It Is Helpful....


Use the type System.DateTime, which supports the operators: '<', '<=', '>', '>=' and '=='.

—SA


这篇关于使用C#检查时间是否存在两次(2 PM-3 Pm)之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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