码迷,mamicode.com
首页 > Web开发 > 详细

.Net 获取日期所属于一年中的第几周

时间:2017-05-26 13:11:55      阅读:433      评论:0      收藏:0      [点我收藏+]

标签:获取   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 获取日期所属于一年中的第几周

标签:获取   http   get   inf   datetime   调用   例子   对象   std   

原文地址:http://www.cnblogs.com/wangqiideal/p/6907828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!