2 回答
TA贡献1853条经验 获得超18个赞
CalendarWeekRule Enumeration
:定义确定一年中第一周的不同规则。
以前你使用的是什么:
CalendarWeekRule.FirstFourDayWeek
需要替换为:
CalendarWeekRule.FirstFullWeek
因为:
代码片段:
public static void Main()
{
var week_test = Convert.ToDateTime("05/06/2018");
var week_test2 = Convert.ToDateTime("05/13/2018");
List<DateTime> weekList = new List<DateTime>();
weekList.Add(week_test);
weekList.Add(week_test2);
CultureInfo ciCurr = CultureInfo.CurrentCulture;
foreach(var week in weekList)
{
int weekNum = ciCurr.Calendar.GetWeekOfYear(week, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday);
Console.WriteLine(weekNum);
}
}
输出:
演示:
TA贡献1798条经验 获得超3个赞
您需要更改方法的参数
CultureInfo.InvariantCulture.Calendar.GetWeekOfYear()
CalendarWeekRule.FirstFourDayWeek -> CalendarWeekRule.FirstFullWeek
根据文档为FirstFullWeek- 表示一年的第一周从该年的第一天或之后指定的一周的第一天的第一次出现开始。
像这样
public static int GetWeekOfYear(DateTime date)
{
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday);
}
- 2 回答
- 0 关注
- 265 浏览
添加回答
举报