本文整理汇总了C#中Calendar.GetDayOfWeek方法的典型用法代码示例。如果您正苦于以下问题:C# Calendar.GetDayOfWeek方法的具体用法?C# Calendar.GetDayOfWeek怎么用?C# Calendar.GetDayOfWeek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Calendar
的用法示例。
在下文中一共展示了Calendar.GetDayOfWeek方法的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: GetDayOfWeek_Invalid
public static void GetDayOfWeek_Invalid(Calendar calendar, DateTime dt)
{
if (calendar is HijriCalendar || calendar is UmAlQuraCalendar || calendar is PersianCalendar || calendar is HebrewCalendar)
{
calendar.GetDayOfWeek(dt);
}
else
{
Assert.Throws<ArgumentOutOfRangeException>("time", () => calendar.GetDayOfWeek(dt));
}
}