如果发现当前时间落在一个时间范围 [英] Find if current time falls in a time range

查看:153
本文介绍了如果发现当前时间落在一个时间范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.NET 3.5

Using .NET 3.5

我想,以确定是否当前时间落在一个时间范围。

I want to determine if the current time falls in a time range.

到目前为止,我有currentime:

So far I have the currentime:

DateTime currentTime = new DateTime();
currentTime.TimeOfDay;

我如何得到该时间范围转换比较消隐。
将这项工作?

I'm blanking out on how to get the time range converted and compared. Would this work?

if (Convert.ToDateTime("11:59") <= currentTime.TimeOfDay 
    && Convert.ToDateTime("13:01") >= currentTime.TimeOfDay)
{
   //match found
}

UPDATE1:谢谢大家对你的建议。我不熟悉的时间跨度功能。

UPDATE1: Thanks everyone for your suggestions. I wasn't familiar with the TimeSpan function.

推荐答案

有关检查日常使用的时间:

For checking for a time of day use:

TimeSpan start = new TimeSpan(10, 0, 0); //10 o'clock
TimeSpan end = new TimeSpan(12, 0, 0); //12 o'clock
TimeSpan now = DateTime.Now.TimeOfDay;

if ((now > start) && (now < end))
{
   //match found
}

有关绝对时间是:

DateTime start = new DateTime(2009, 12, 9, 10, 0, 0)); //10 o'clock
DateTime end = new DateTime(2009, 12, 10, 12, 0, 0)); //12 o'clock
DateTime now = DateTime.Now;

if ((now > start) && (now < end))
{
   //match found
}

这篇关于如果发现当前时间落在一个时间范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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