.Net 获取日期所属于一年中的第几周
關鍵代碼:
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());
輸出結果:
?
轉載于:https://www.cnblogs.com/wangqiideal/p/6907828.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的.Net 获取日期所属于一年中的第几周的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jenkins学习
- 下一篇: 关于微信小程序的的总结