標籤:擷取 http get inf datetime 調用 例子 對象 std
關鍵代碼:
public static int WeekOfYear(DateTime dt, CultureInfo ci)
{
//強制設定周一是每周的第一天
return ci.Calendar.GetWeekOfYear(dt, ci.DateTimeFormat.CalendarWeekRule, DayOfWeek.Monday);
//根據地區性資訊對象設定每周的第一天
//return ci.Calendar.GetWeekOfYear(dt, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
}
調用例子:
CultureInfo cultInfo = CultureInfo.GetCultureInfo("zh-CN");
int week = WeekOfYear(DateTime.Now, cultInfo);
Console.WriteLine("Date={0},WeekOfYear={1},day ( of the week) is={2}", DateTime.Now.ToString(), week, DateTime.Now.DayOfWeek.ToString());
string dateStr = "2017-01-01";
DateTime date1 = DateTime.Parse(dateStr, cultInfo.DateTimeFormat);
week = WeekOfYear(date1, cultInfo);
Console.WriteLine("Date={0},WeekOfYear={1},day ( of the week) is={2}", dateStr, week,date1.DayOfWeek.ToString());
dateStr = "2017-12-31";
date1 = DateTime.Parse(dateStr, cultInfo.DateTimeFormat);
week = WeekOfYear(date1, cultInfo);
Console.WriteLine("Date={0},WeekOfYear={1},day ( of the week) is={2}", dateStr, week, date1.DayOfWeek.ToString());
輸出結果:
.Net 擷取日期所屬於一年中的第幾周