2013-09-30 67 views
1

我知道這個問題已被問了很多次,但我有一個小的轉折。 有許多不同班次的工作,我有兩個字符串shiftStartshiftEnd。 (例如「6:00:00 PM」&「03:00:00 AM」)。這代表中午至中午。 我想做一個函數告訴我DateTime.Now是否在這個時間週期內。當前時間在範圍內

注:移位可以是任何東西 ...早晨至中午,中午到凌晨,沒有固定的時間(如8小時)。

static bool NowWithinShiftTime("6:00:00 PM", "03:00:00 AM") 

以下是我所嘗試,但無論我做什麼,我似乎無法找到邏輯... 請幫我...

static bool NowWithinShiftTime(string shiftStart, string shiftEnd) 
    { 
     DateTime startDate; 
     DateTime endDate; 
     DateTime now = DateTime.Now; 

     TimeSpan startTime = DateTime.Parse(shiftStart).TimeOfDay; 
     TimeSpan endTime = DateTime.Parse(shiftEnd).TimeOfDay; 

     if (startTime < endTime) // same day 
     { 
      startDate = new DateTime(now.Year, now.Month, now.Day) + startTime; 
      endDate = new DateTime(now.Year, now.Month, now.Day) + endTime; 
     } 
     else // next day 
     { 
      startDate = new DateTime(now.Year, now.Month, now.AddDays(-1).Hour) + startTime; 
      endDate = DateTime.Today.AddDays(1) + endTime; 
     } 
     if (now >= startDate && now <= endDate) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
+0

'now.AddDays(1)·小時'< - 什麼?你爲什麼要這樣做? – Katniss

+0

@ 666bytes:你應該更喜歡'startDate = DateTime.Today + startTime',更短更清晰。對於明天,像往常一樣「AddDays(1)」。 – Jon

回答

6

您可以用TimeSpan堅持:

static bool NowWithinShiftTime(string shiftStart, string shiftEnd) 
{ 
    TimeSpan startTime = DateTime.Parse(shiftStart).TimeOfDay; 
    TimeSpan endTime = DateTime.Parse(shiftEnd).TimeOfDay; 
    TimeSpan now = DateTime.Now.TimeOfDay; 

    if (startTime < endTime) 
     return now >= startTime && now <= endTime; 
    else 
     return now >= startTime || now <= endTime; 
} 
+0

@ChrisWue:你說得對。現在修復。 – Douglas

+1

@ 666bytes如果可能的話,如果該函數接受TimeSpan而不是字符串,而不是執行解析,那將會更好。 –

+0

@Douglas感謝我的朋友。真的很感謝... –

1

我相信你90%的有:

static bool NowWithinShiftTime(string shiftStart, string shiftEnd) 
{ 
    DateTime startDate; 
    DateTime endDate; 
    DateTime now = DateTime.Now; 

    TimeSpan startTime = DateTime.Parse(shiftStart).TimeOfDay; 
    TimeSpan endTime = DateTime.Parse(shiftEnd).TimeOfDay; 

    if (startTime < endTime) // same day 
    { 
     startDate = DateTime.Today + startTime; 
     endDate = DateTime.Today + endTime; 
    } 
    else // next day 
    { 
     startDate = DateTime.Today + startTime; 
     endDate = DateTime.Today.AddDays(1) + endTime; 
    } 
    if (now >= startDate && now <= endDate) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

下面是一個ideone展示它的工作原理:http://ideone.com/rdb867

+0

這不會工作,因爲到達12:00:00上午startDate也將成爲下一個日期。 –

+0

在上午12:00,日期也會改變,所以這不是問題。 –

+0

我調整了這個例子a * tiny * bit,得到了我需要的!謝謝!! –

6

首先,讓所有的業務邏輯交易只是時間相關的類型。 (我個人使用我的Noda Time庫,它的LocalTime類型用於這項工作,但...)解析可以在調用代碼中。

邏輯相當簡單:如果「結束時間」實際上是在「開始時間」之前,那麼只需切換兩輪並反轉結果即可。畢竟,你只是將一天劃分爲「換班時間」和「不換班時間」......每當上午3點到下午8點的班次都「關閉」時,(假設)晚上8點到凌晨3點的班次就會「開」。

例如:

public bool NowWithinShiftTime(TimeSpan startTimeOfDay, TimeSpan endTimeOfDay) 
{ 
    if (startTimeOfDay > endTimeOfDay) 
    { 
     return !NowWithinShiftTime(endTimeOfDay, startTimeOfDay); 
    } 
    TimeSpan now = DateTime.Now.TimeOfDay; 
    return startTimeOfDay <= now && now < endTimeOfDay; 
} 

注:

  • 我做了這個故意半封閉,因爲這往往是適當的間隔,這樣如果你有對接的時間間隔,任何給定的價值恰好落入一個。檢查你想要的,但它只會與這兩個精確每天的時間點相關。
  • 該代碼目前很難測試:我鼓勵您將「時鐘」的概念抽象爲一個界面,以便您可以使用假時鐘進行測試。 (野田的時間爲您完成此,並提供適當的FakeClock。)
+0

這很好... –

0

這應該工作:

bool IsWithinTimeRange(DateTime dateToCompare, DateTime startDate, DateTime endDate) 
{ 
    TimeSpan timeToCompare = dateToCompare.TimeOfDay; 
    TimeSpan start = startDate.TimeOfDay; 
    TimeSpan end = endDate.TimeOfDay; 

    if (start < end) 
     return timeToCompare >= start && timeToCompare <= end; 
    else 
     return !(end < timeToCompare && timeToCompare < start); 
} 
1

這應該工作

static bool NowWithinShiftTime(string shiftStart, string shiftEnd) 
{ 
    DateTime now = DateTime.Now; 
    DateTime startDate = DateTime.Parse(shiftStart); 
    DateTime endDate = DateTime.Parse(shiftEnd); 

    if (endDate < startDate) 
    { 
     endDate = endDate.AddDays(1); 
    } 

    return now >= startDate && now <= endDate; 
} 
+0

它不。 'shiftStart'和'shiftEnd'沒有日期信息,所以'now'總是大於'endDate'。 – recursive

+0

@recursive你確定嗎?如果在應用解析當前日期時未指定日期 –

+0

@recursive我測試過了,它完美地工作。你能否在任何測試案例中出現失敗的地方? –

相關問題