本文整理汇总了C#中Calendar.GetWeekOfYear方法的典型用法代码示例。如果您正苦于以下问题:C# Calendar.GetWeekOfYear方法的具体用法?C# Calendar.GetWeekOfYear怎么用?C# Calendar.GetWeekOfYear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Calendar
的用法示例。
在下文中一共展示了Calendar.GetWeekOfYear方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDayOfYearHelper
private static int GetDayOfYearHelper(Calendar calendar, DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
{
int weekOfYear = calendar.GetWeekOfYear(time, rule, firstDayOfWeek);
int dayOfYear = calendar.GetDayOfYear(time); // 1-based
int dayOfWeek = calendar.GetDayOfWeek(time) - firstDayOfWeek + 1; // 1-based
if (dayOfWeek <= 0)
dayOfWeek += DaysPerWeek; // Make it a positive value
int dayOfWeekForJan1 = dayOfWeek - (dayOfYear - 1) % DaysPerWeek; // 1-based
if (dayOfWeekForJan1 <= 0)
dayOfWeekForJan1 += DaysPerWeek; // Make it a positive value
// When the day of specific time falls on the previous year,
// return the number of days from January 1 directly.
// There could be 6 weeks within a month.
if (time.Month == 1 && weekOfYear > 6)
{
return dayOfWeek - dayOfWeekForJan1 + 1;
}
int offset = 0;
switch (rule)
{
case CalendarWeekRule.FirstDay:
offset = dayOfWeek - dayOfWeekForJan1;
break;
case CalendarWeekRule.FirstFourDayWeek:
if (dayOfWeekForJan1 <= 4)
{
offset = dayOfWeek - dayOfWeekForJan1;
}
else
{
offset = dayOfWeek + DaysPerWeek - dayOfWeekForJan1;
}
break;
case CalendarWeekRule.FirstFullWeek:
if (dayOfWeekForJan1 == 1)
{
offset = dayOfWeek - dayOfWeekForJan1;
}
else
{
offset = dayOfWeek + DaysPerWeek - dayOfWeekForJan1;
}
break;
}
return (weekOfYear - 1) * DaysPerWeek + offset + 1;;
}
示例2: GetWeekOfYear_Invalid
public static void GetWeekOfYear_Invalid(Calendar calendar)
{
// Rule is outside supported range
Assert.Throws<ArgumentOutOfRangeException>("rule", () => calendar.GetWeekOfYear(calendar.MaxSupportedDateTime, CalendarWeekRule.FirstDay - 1, DayOfWeek.Saturday));
Assert.Throws<ArgumentOutOfRangeException>("rule", () => calendar.GetWeekOfYear(calendar.MaxSupportedDateTime, CalendarWeekRule.FirstFourDayWeek + 1, DayOfWeek.Saturday));
// FirstDayOfWeek is outside supported range
Assert.Throws<ArgumentOutOfRangeException>("firstDayOfWeek", () => calendar.GetWeekOfYear(calendar.MaxSupportedDateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday - 1));
Assert.Throws<ArgumentOutOfRangeException>("firstDayOfWeek", () => calendar.GetWeekOfYear(calendar.MaxSupportedDateTime, CalendarWeekRule.FirstDay, DayOfWeek.Saturday + 1));
}