2011-02-10 74 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)); 
     } 
    }