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;
更新 2016 年 6 月 8 日
これは実用的なソリューションであるため、反対票が適切であった理由は不明です。OP は特に を要求しましたが、@Gabe の回答に従って代わりにDateTime
使用することをお勧めします。TimeSpan
これが私の答えによる機能です:
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;
}
}