[C#] 一些處理日期用的好用function

2014-07-28

最近在處理一些專案需要計算關於周的問題在台灣,一周的第一天普遍認為是周一,但是外國人都是用週日當作一個禮拜的第一天

所以會有羞誤差,下面提供三支function 自己記錄一下

1.隨便給一個日期取的該日期當周的第一天(也就是星期一)


/// <summary>
/// 取得該天的那一周的第一天
/// 請注意此算法的方法周一為一個禮拜的第一天
/// </summary>
/// <param name="checkDate"></param>
/// <returns></returns>
public static DateTime GetWeekFirstDate(DateTime checkDate)
{
    var q = (int)checkDate.DayOfWeek;
    return q == 0 ? checkDate.AddDays(q - 6) : checkDate.AddDays(-(q - 1));
}

測試:
Response.Write(GetWeekFirstDate(new DateTime(2014, 7, 27)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekFirstDate(new DateTime(2014, 7, 28)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekFirstDate(new DateTime(2014, 7, 29)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekFirstDate(new DateTime(2014, 7, 30)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekFirstDate(new DateTime(2014, 7, 31)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekFirstDate(new DateTime(2014, 8, 1)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekFirstDate(new DateTime(2014, 8, 2)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekFirstDate(new DateTime(2014, 8, 3)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekFirstDate(new DateTime(2014, 8, 4)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

結果:
2014/07/21 00:00:00
2014/07/28 00:00:00
2014/07/28 00:00:00
2014/07/28 00:00:00
2014/07/28 00:00:00
2014/07/28 00:00:00
2014/07/28 00:00:00
2014/07/28 00:00:00
2014/08/04 00:00:00

2.隨便給一個日期取得該日期當周的最後一天(也就是周日),請注意後面那個isSetTo2359 預設開啟,會把回傳的日期直接時間點設到該周日的23:59:59


/// <summary>
/// 取得該天當周的最後一天(週日)
/// 請注意此算法的方法周一為一個禮拜的第一天,所以最後一天為周日
/// </summary>
/// <param name="checkDate"></param>
/// <param name="isSetTo2359">是否將回傳日期直接設成23點59分59秒</param>
/// <returns></returns>
public static DateTime GetWeekEndDate(DateTime checkDate, bool isSetTo2359 = true)
{
    var q = (int)checkDate.DayOfWeek;
    var res = q == 0 ? checkDate : checkDate.AddDays(7 - q);
    return isSetTo2359 ? new DateTime(res.Year, res.Month, res.Day, 23, 59, 59, 999) : new DateTime(res.Year, res.Month, res.Day, 0, 0, 0, 0);  
}

測試:
Response.Write(GetWeekEndDate(new DateTime(2014, 7, 27)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekEndDate(new DateTime(2014, 7, 28)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekEndDate(new DateTime(2014, 7, 29)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekEndDate(new DateTime(2014, 7, 30)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekEndDate(new DateTime(2014, 7, 31)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekEndDate(new DateTime(2014, 8, 1)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekEndDate(new DateTime(2014, 8, 2)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekEndDate(new DateTime(2014, 8, 3)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

Response.Write(GetWeekEndDate(new DateTime(2014, 8, 4)).ToString("yyyy/MM/dd HH:mm:ss") + "<br />");

結果:
2014/07/27 23:59:59
2014/08/03 23:59:59
2014/08/03 23:59:59
2014/08/03 23:59:59
2014/08/03 23:59:59
2014/08/03 23:59:59
2014/08/03 23:59:59
2014/08/03 23:59:59
2014/08/10 23:59:59

3.隨便給一天,取得該日期的當周為該年的第幾周


/// <summary>
 /// 取的該天的當周是該年的第幾周
 /// 請注意此算法的方法周一為一個禮拜的第一天
 /// </summary>
 /// <param name="checkDate"></param>
 /// <returns></returns>
 public static int GetTheDayWeekOfYEar(DateTime checkDate)
 {
     var day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(checkDate);
     if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
     {
         checkDate = checkDate.AddDays(3);
     }
     return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(checkDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
 } 

測試:
Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 7, 27)) + "<br />");

Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 7, 28)) + "<br />");

Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 7, 29)) + "<br />");

Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 7, 30)) + "<br />");

Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 7, 31)) + "<br />");

Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 8, 1)) + "<br />");

Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 8, 2)) + "<br />");

Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 8, 3)) + "<br />");

Response.Write(GetTheDayWeekOfYEar(new DateTime(2014, 8, 4)) + "<br />");

結果:
30
31
31
31
31
31
31
31
32

很簡單,使用前請先注意是不是自己要的需求。

周計算參考文件: http://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date


當麻許的超技八 2014 | Donma Hsu Design.