[Implement] 取得日期週次

[Implement] 取得日期週次

取得該日期的週次

寫法一

{
    System.Globalization.GregorianCalendar getWeek = new
    System.Globalization.GregorianCalendar();

    return getWeek.GetWeekOfYear(dt, System.Globalization.CalendarWeekRule.FirstDay,
        DayOfWeek.Sunday);//設定星期日為一週的第一天
}

寫法二

{
    DateTime end = new DateTime(year, 12, 31);//設定該年的最後一天日期
        System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();

    return gc.GetWeekOfYear(end, System.Globalization.CalendarWeekRule.FirstDay,
        DayOfWeek.Sunday);//設定星期日為一週的第一天
}

用法

DateTime date2 = new DateTime(2011, 3, 15, 1, 2, 3);//年 月 日 小時 分 秒

int week1 = getWeekOfYear(date1); //12

int week2 = getWeekOfYear(date2); //12

int week3 = getWeekAmount(2011); //53