2011-02-10 79 views
3

我正在做一个函数来检查24小时格式的时间范围之间的时间差,但是我的代码有一些问题,有没有人可以指出如何解决?当时间落在时间范围之间时,返回True?

我的代码:

bool isDoTime(int starthour, int startminute, int endhour, int endminute) 
    { 
     TimeSpan start = new TimeSpan(starthour, startminute, 0); 
     TimeSpan end = new TimeSpan(endhour, endminute, 0); 
     TimeSpan add24h = new TimeSpan(24, 0, 0); 
     TimeSpan now = DateTime.Now.TimeOfDay; 


     if (starthour > endhour || (endhour == starthour && endminute <= startminute)) 
     { 
      end += add24h; 
     } 
     if ((now > start) && (now < end)) 
     { 
      return true; 
     } 

     return false; 
    } 

问题:我想至20:30之间返回true时,当前时间 - 下午3:30,但是当我下面我的代码运行。条件是返回true只有从8:30到00:00,从00:00不是真的 - 3:30

if (isDoTime(20,30,3,30) //return true from 20:30 - 3:30 
{ 
    //dosomething 
} 
+0

的问题是,你是转换3:30结束时间为27:30,但你不2:30当前时间转换成26 :30。 – 2011-02-10 11:51:14

回答

5

在一个检查分手了,如果它地跨midninght,一个是同一天。

TimeSpan start = new TimeSpan(starthour, startminute, 0); 
TimeSpan end = new TimeSpan(endhour, endminute, 0); 
TimeSpan now = DateTime.Now.TimeOfDay; 

//The readable version: 
if(start>end){ 
    //Must check if after start (before midnight) or before end (after midnight) 
    if((now > start) || (now < end)){ 
    return true; 
    { 
} 
else 
{ 
    //Simple check - span is within same day 
    if ((now > start) && (now < end)) 
    { 
    return true; 
    } 
} 
return false; 

短/神秘版本:

return start > end ? (now > start) || (now < end) : (now > start) && (now < end); 
0

这将是更容易做出在这里使用的DateTime的作为参数,并避免整个手动检查的问题:

bool isDoTime(DateTime starttime, DateTime endtime) 
{ 
    if (DateTime.Now > starttime && DateTime.Now < endtime) 
    { 
     return true; 
    } 
    return false; 
} 
+0

如果开始时间大于结束时间,则OP仍需要将24小时添加到结束时间。 – ChrisF 2011-02-10 11:32:09

1

我想你会更好使用DateTime但是,你仍然需要检查,如果开始时间比结束时间更大,在这种情况下,增加24小时。

你的方法将启动:

bool isDoTime(int starthour, int startminute, int endhour, int endminute) 
{ 
    DateTime start = new DateTime(0, 0, 0, starthour, startminute, 0); 
    DateTime end = new DateTime(0, 0, 0, endhour, endminute, 0); 
    if (start > end) 
    { 
     end.AddDays(1); 
    } 
    DateTime now = DateTime.Now; 

    return start < now && now < end; 
} 

虽然你可能会根据你的逻辑

<=测试(除非它是你的,你要添加24小时当然,其中的逻辑 - 它的代码执行?)

3

您想要使用DateTime结构而不是整数。您也可以将其推广到任意日期时间。如果secondTime小于firstTime,则为secondTime增加1天。

public bool IsBetween(this DateTime thisTime, DateTime firstTime, DateTime secondTime) { 
    if (secondTime < firstTime) 
     secondTime = secondTime.AddDays(1); 
    return firstTime < thisTime && thisTime < secondTime); 
} 

// to use... 
bool isDoTime = DateTime.Now.IsBetween(firstTime, secondTime); 
+0

开始时间大于结束时间的情况如何? – ChrisF 2011-02-10 11:34:05

0
[TestFixture] 
    public class Class1 
    { 
     private DateTime _now; 

     [SetUp] 
     public void SetUp() 
     { 
      _now = DateTime.MinValue.AddDays(1).AddHours(2); //02.01.0001 02:00:00 
     } 

     [Test] 
     public void TestCase() 
     { 
      Assert.IsTrue(IsDoTime(20, 30, 3, 30)); 
     } 

     bool IsDoTime(int starthour, int startminute, int endhour, int endminute) 
     { 
      var start1 = DateTime.MinValue.AddHours(starthour).AddMinutes(startminute); //01.01.0001 20:30:00 

      var end1 = endhour < starthour 
          ? DateTime.MinValue.AddDays(1).AddHours(endhour).AddMinutes(endminute) //02.01.0001 03:30:00 
          : DateTime.MinValue.AddHours(endhour).AddMinutes(endminute); 

      return ((_now > start1) && (_now < end1)); 
     } 
    }