对于我的应用我需要知道,如果Now()
是两个值之间。
用户可以设置开始和结束时间,所以他不会被通知干扰(在夜间为例)。
因此,如果已经有两个TimePicker
S(开始和结束时间),用户可以设置。
比方说,用户设置22:00
的StartTime
和07:00
的EndTime
(这将包括夜间)。
如何检查是否DateTime.Now
是所选择的开始和结束时间之间?
编辑:我只是想这与小时和分钟部分的工作。 因此,如果用户设置开始和结束时间应该工作的每一个夜晚。
对于我的应用我需要知道,如果Now()
是两个值之间。
用户可以设置开始和结束时间,所以他不会被通知干扰(在夜间为例)。
因此,如果已经有两个TimePicker
S(开始和结束时间),用户可以设置。
比方说,用户设置22:00
的StartTime
和07:00
的EndTime
(这将包括夜间)。
如何检查是否DateTime.Now
是所选择的开始和结束时间之间?
编辑:我只是想这与小时和分钟部分的工作。 因此,如果用户设置开始和结束时间应该工作的每一个夜晚。
首先,你需要的一切转换成同一单位(我们将使用TimeSpan
),那么你需要看到的启动结束时间是否越过午夜,最后以此为基础进行检查的结果,你的比较:
// 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);
这里有一个函数来为你做它:
bool TimeBetween(DateTime datetime, TimeSpan start, TimeSpan end)
{
// convert datetime to a TimeSpan
TimeSpan now = datetime.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);
}
你会这样称呼它:
bool silenceAlarm = TimeBetween(DateTime.Now, StartTime.Value, EndTime.Value);
由于您只收集两次没有日期,你需要弄清楚如果这两个时间是从当天与否。 如果你把StartTime
, EndTime
,以及Now
到TimeSpans
:
if (StartTime > EndTime)
{
// the range crosses midnight, do the comparisons independently
return (StartTime < Now) || (Now < EndTime);
}
else
{
// the range is on the same day, both comparisons must be true
return StartTime < Now && Now < EndTime;
}
public static bool isCurrenctDateBetween(DateTime fromDate, DateTime toDate)
{
DateTime curent = DateTime.Now.Date;
if (fromDate.CompareTo(toDate) >= 1)
{
MessageBox.Show("From Date shouldn't be grater than To Date", "DateRange",MessageBoxButton.OKCancel, MessageBoxImage.Warning);
}
int cd_fd = curent.CompareTo(fromDate);
int cd_td = curent.CompareTo(toDate);
if (cd_fd == 0 || cd_td == 0)
{
return true;
}
if (cd_fd >= 1 && cd_td <= -1)
{
return true;
}
return false;
}
DateTime nowDate = DateTime.Now;
// set these to today + time from time picker
DateTime startDate = new DateTime(nowDate.Year, nowDate.Month, nowDate.Day,
selectedStart.Hour, selectedStart.Minute, 0);
DateTime endDate = new DateTime(nowDate.Year, nowDate.Month, nowDate.Day,
selectedEnd.Hour, selectedEnd.Minute, 0);
bool isBetween = nowDate < endDate && nowDate > startDate;
更新08军-2016
不知道为什么downvote是适当的,因为这是一个可行的解决方案。 该任择议定书没有明确的提出DateTime
,但是我不建议使用TimeSpan
,而不是按照由@Gabe答案。
这里有一个工作职能按我的答案:
public static bool TimeBetween(DateTime check, DateTime start, DateTime end, bool inclusive = true)
{
var from = new DateTime(check.Year, check.Month, check.Day,
start.Hour, start.Minute, start.Second, start.Millisecond);
var to = new DateTime(check.Year, check.Month, check.Day,
end.Hour, end.Minute, end.Second, end.Millisecond);
if (inclusive)
return from <= check && to >= check;
return from < check && to > check;
}
这里有一个工作小提琴: https://dotnetfiddle.net/vZCXqv 。
全码:
using System;
public class Program
{
public static void Main()
{
var start = new DateTime(1, 1, 1, 9, 0, 0);
var end = new DateTime(1, 1, 1, 17, 0, 0);
Console.WriteLine("{0} - Too early", TimeBetween(new DateTime(2014, 1, 1, 08, 59, 59, 999), start, end));
Console.WriteLine("{0} - On start time exclusive", TimeBetween(new DateTime(2014, 1, 1, 09, 00, 00, 000), start, end, false));
Console.WriteLine("{0} - On start time inclusive", TimeBetween(new DateTime(2014, 1, 1, 09, 00, 00, 000), start, end));
Console.WriteLine("{0} - After start time", TimeBetween(new DateTime(2014, 1, 1, 09, 00, 00, 001), start, end));
Console.WriteLine("{0} - Before end time", TimeBetween(new DateTime(2014, 1, 1, 16, 59, 59, 999), start, end));
Console.WriteLine("{0} - On end time inclusive", TimeBetween(new DateTime(2014, 1, 1, 17, 00, 00, 000), start, end));
Console.WriteLine("{0} - On end time exclusive", TimeBetween(new DateTime(2014, 1, 1, 17, 00, 00, 000), start, end, false));
Console.WriteLine("{0} - Too late", TimeBetween(new DateTime(2014, 1, 1, 17, 00, 00, 001), start, end));
}
public static bool TimeBetween(DateTime check, DateTime start, DateTime end, bool inclusive = true)
{
var from = new DateTime(check.Year, check.Month, check.Day, start.Hour, start.Minute, start.Second, start.Millisecond);
var to = new DateTime(check.Year, check.Month, check.Day, end.Hour, end.Minute, end.Second, end.Millisecond);
if (inclusive)
return from <= check && to >= check;
return from < check && to > check;
}
}
的杜佩查找,如果目前的时间落在一个时间范围
DateTime start = new DateTime(2009, 12, 9, 10, 0, 0));
DateTime end = new DateTime(2009, 12, 10, 12, 0, 0));
DateTime now = DateTime.Now;
if ((now > start) && (now < end))
{
//match found
}
时间跨度,再次,从重复数据删除拍摄。
TimeSpan start = new TimeSpan(10, 0, 0);
TimeSpan end = new TimeSpan(12, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
if ((now > start) && (now < end))
{
//match found
}
我用这个,这样你就可以直接传递日期时间:
public static bool TimeBetween(DateTime time, DateTime startDateTime, DateTime endDateTime)
{
// get TimeSpan
TimeSpan start = new TimeSpan(startDateTime.Hour, startDateTime.Minute, 0);
TimeSpan end = new TimeSpan(endDateTime.Hour, endDateTime.Minute, 0);
// convert datetime to a TimeSpan
TimeSpan now = time.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);
}
只要做直接比较。
如果(日期> STARTDATE &&日期<结束日期)