本文整理汇总了C#中System.Globalization.Calendar.GetDayOfWeek方法的典型用法代码示例。如果您正苦于以下问题:C# Calendar.GetDayOfWeek方法的具体用法?C# Calendar.GetDayOfWeek怎么用?C# Calendar.GetDayOfWeek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Globalization.Calendar
的用法示例。
在下文中一共展示了Calendar.GetDayOfWeek方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDateFormatValues
protected virtual void GetDateFormatValues(DateParts parts, Calendar calendar, out int twoDigitYear, out string monthName, out string monthNameShort, out string monthNameGenitive, out string monthNameShortGenitive, out string dayName, out string dayNameShort, out string eraName, out string eraNameShort) {
var yearString = parts.Year.ToString("00", System.Globalization.CultureInfo.InvariantCulture);
twoDigitYear = Int32.Parse(yearString.Substring(yearString.Length - 2));
monthName = parts.Month > 0 ? _dateTimeFormatProvider.MonthNames[parts.Month - 1] : null;
monthNameShort = parts.Month > 0 ? _dateTimeFormatProvider.MonthNamesShort[parts.Month - 1] : null;
monthNameGenitive = parts.Month > 0 ? _dateTimeFormatProvider.MonthNamesGenitive[parts.Month - 1] : null;
monthNameShortGenitive = parts.Month > 0 ? _dateTimeFormatProvider.MonthNamesShortGenitive[parts.Month - 1] : null;
dayName = parts.Day > 0 ? _dateTimeFormatProvider.DayNames[(int)calendar.GetDayOfWeek(parts.ToDateTime(calendar))] : null;
dayNameShort = parts.Day > 0 ? _dateTimeFormatProvider.DayNamesShort[(int)calendar.GetDayOfWeek(parts.ToDateTime(calendar))] : null;
eraName = parts.Day > 0 ? _dateTimeFormatProvider.GetEraName((int)calendar.GetEra(parts.ToDateTime(calendar))) : null;
eraNameShort = parts.Day > 0 ? _dateTimeFormatProvider.GetShortEraName((int)calendar.GetEra(parts.ToDateTime(calendar))) : null;
}
示例2: GetDayOfYear
private int GetDayOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek, int weekOfYear, Calendar myCalendar)
{
int retVal = -367;
int offset = 0;
int dayOfWeekForJan1, dayOfYear, dayOfWeek;
dayOfYear = myCalendar.GetDayOfYear(time); //1-based
dayOfWeek = (int)myCalendar.GetDayOfWeek(time) - (int)firstDayOfWeek + 1; //1-based
if (dayOfWeek <= 0)
dayOfWeek += c_DAYS_PER_WEEK; //Make it a positive value
dayOfWeekForJan1 = dayOfWeek - (dayOfYear - 1) % c_DAYS_PER_WEEK; //1-based
if (dayOfWeekForJan1 <= 0)
dayOfWeekForJan1 += c_DAYS_PER_WEEK; //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;
}
switch (rule)
{
case CalendarWeekRule.FirstDay:
offset = dayOfWeek - dayOfWeekForJan1;
break;
case CalendarWeekRule.FirstFourDayWeek:
if (dayOfWeekForJan1 <= 4)
{
offset = dayOfWeek - dayOfWeekForJan1;
}
else
{
offset = dayOfWeek + c_DAYS_PER_WEEK - dayOfWeekForJan1;
}
break;
case CalendarWeekRule.FirstFullWeek:
if (dayOfWeekForJan1 == 1)
{
offset = dayOfWeek - dayOfWeekForJan1;
}
else
{
offset = dayOfWeek + c_DAYS_PER_WEEK - dayOfWeekForJan1;
}
break;
}
retVal = (weekOfYear - 1) * c_DAYS_PER_WEEK + offset + 1;
return retVal;
}
示例3: Verification
private void Verification(Calendar calendar, DateTime time, DayOfWeek expected, string errorno)
{
DayOfWeek RealVal = calendar.GetDayOfWeek(time);
Assert.Equal(expected, RealVal);
}
示例4: NextValid
//.........这里部分代码省略.........
// Get days and months.
IEnumerable<int> days = day.Days().OrderBy(dy => dy);
IEnumerable<int> months = month.Months();
// Remove months where the first day isn't in the month.
int firstDay = days.First();
if (firstDay > 28)
{
// 2000 is a leap year, so February has 29 days.
months = months.Where(mn => calendar.GetDaysInMonth(2000, mn) >= firstDay);
if (months.Count() < 1)
return DateTime.MaxValue;
}
// Get remaining date components.
int y = calendar.GetYear(dateTime);
int m = calendar.GetMonth(dateTime);
int d = calendar.GetDayOfMonth(dateTime);
int h = calendar.GetHour(dateTime);
int n = calendar.GetMinute(dateTime);
int s = calendar.GetSecond(dateTime);
IEnumerable<int> weeks = week.Weeks();
IEnumerable<DayOfWeek> weekDays = weekDay.WeekDays();
IEnumerable<int> hours = hour.Hours().OrderBy(i => i);
IEnumerable<int> minutes = minute.Minutes().OrderBy(i => i);
IEnumerable<int> seconds = second.Seconds();
do
{
foreach (int currentMonth in months)
{
if (currentMonth < m)
continue;
if (currentMonth > m)
{
d = 1;
h = n = s = 0;
}
m = currentMonth;
foreach (int currentDay in days)
{
if (currentDay < d)
continue;
if (currentDay > d)
h = n = s = 0;
d = currentDay;
// Check day is valid for this month.
if ((d > 28) && (d > calendar.GetDaysInMonth(y, m)))
break;
// We have a potential day, check week and week day
dateTime = new DateTime(y, m, d, h, n, s);
if ((week != Week.Every) &&
(!weeks.Contains(dateTime.WeekNumber(calendar, calendarWeekRule, firstDayOfWeek))))
continue;
if ((weekDay != WeekDay.Every) &&
(!weekDays.Contains(calendar.GetDayOfWeek(dateTime))))
continue;
// We have a date match, check time.
foreach (int currentHour in hours)
{
if (currentHour < h) continue;
if (currentHour > h)
n = s = 0;
h = currentHour;
foreach (int currentMinute in minutes)
{
if (currentMinute < n) continue;
if (currentMinute > n)
s = 0;
n = currentMinute;
foreach (int currentSecond in seconds)
{
if (currentSecond < s) continue;
return new DateTime(y, m, d, h, n, currentSecond, calendar);
}
n = s = 0;
}
h = n = s = 0;
}
d = 1;
}
d = 1;
h = n = s = 0;
}
y++;
// Don't bother checking max year.
if (y > 9998)
return DateTime.MaxValue;
// Start next year
m = d = 1;
h = n = s = 0;
} while (true);
}
示例5: GetFirstDayOfWeek
/// <summary>
/// Gets the first day of a week.
/// </summary>
/// <param name="year">The year.</param>
/// <param name="week">The week.</param>
/// <param name="calendar">The calendar.</param>
/// <param name="calendarWeekRule">The calendar week rule.</param>
/// <param name="firstDayOfWeek">The first day of week.</param>
/// <returns>The date of the first day in the week.</returns>
public static DateTime GetFirstDayOfWeek(
int year,
int week,
Calendar calendar = null,
CalendarWeekRule calendarWeekRule = CalendarWeekRule.FirstFourDayWeek,
DayOfWeek firstDayOfWeek = DayOfWeek.Sunday)
{
if (calendar == null)
calendar = CultureInfo.CurrentCulture.Calendar;
DateTime firstDay = new DateTime(year, 1, 7, calendar);
while (calendar.GetDayOfWeek(firstDay) != firstDayOfWeek || WeekNumber(firstDay, calendar, calendarWeekRule, firstDayOfWeek) % 52 > 1)
{
firstDay = calendar.AddDays(firstDay, -1);
}
// Add the week
return (week != 1) ? calendar.AddWeeks(firstDay, week - 1) : firstDay;
}
示例6: InternalGetWeekOfYearFullDays
// This is copied from the generic implementation of GetWeekOfYearFullDays() in Calendar.cs. The generic implementation is not good enough
// in the case of FristFullWeek and FirstFourDayWeek since it needs the data for B.C. year 1 near 0001/1/1.
// We override the generic implementation to handle this special case.
// Parameters
//
internal static int InternalGetWeekOfYearFullDays(Calendar cal, DateTime time, int firstDayOfWeek, int fullDays, int daysOfMinYearMinusOne) {
int dayForJan1;
int offset;
int day;
int dayOfYear = cal.GetDayOfYear(time) - 1; // Make the day of year to be 0-based, so that 1/1 is day 0.
//
// Calculate the number of days between the first day of year (1/1) and the first day of the week.
// This value will be a positive value from 0 ~ 6. We call this value as "offset".
//
// If offset is 0, it means that the 1/1 is the start of the first week.
// Assume the first day of the week is Monday, it will look like this:
// Sun Mon Tue Wed Thu Fri Sat
// 12/31 1/1 1/2 1/3 1/4 1/5 1/6
// +--> First week starts here.
//
// If offset is 1, it means that the first day of the week is 1 day ahead of 1/1.
// Assume the first day of the week is Monday, it will look like this:
// Sun Mon Tue Wed Thu Fri Sat
// 1/1 1/2 1/3 1/4 1/5 1/6 1/7
// +--> First week starts here.
//
// If offset is 2, it means that the first day of the week is 2 days ahead of 1/1.
// Assume the first day of the week is Monday, it will look like this:
// Sat Sun Mon Tue Wed Thu Fri Sat
// 1/1 1/2 1/3 1/4 1/5 1/6 1/7 1/8
// +--> First week starts here.
// Day of week is 0-based.
// Get the day of week for 1/1. This can be derived from the day of week of the target day.
// Note that we can get a negative value. It's ok since we are going to make it a positive value when calculating the offset.
dayForJan1 = (int)cal.GetDayOfWeek(time) - (dayOfYear % 7);
// Now, calucalte the offset. Substract the first day of week from the dayForJan1. And make it a positive value.
offset = (firstDayOfWeek - dayForJan1 + 14) % 7;
if (offset != 0 && offset >= fullDays)
{
//
// If the offset is greater than the value of fullDays, it means that
// the first week of the year starts on the week where Jan/1 falls on.
//
offset -= 7;
}
//
// Calculate the day of year for specified time by taking offset into account.
//
day = dayOfYear - offset;
if (day >= 0) {
//
// If the day of year value is greater than zero, get the week of year.
//
return (day/7 + 1);
}
//
// Otherwise, the specified time falls on the week of previous year.
// Note that it is not always week 52 or 53, because it depends on the calendar. Different calendars have different number of days in a year.
//
// Repeat the previous calculation logic using the previous year and calculate the week of year for the last day of previous year.
int year = cal.GetYear(time);
if (year <= cal.GetYear(cal.MinSupportedDateTime)) {
// This specified time is in 0001/1/1 ~ 0001/1/7.
dayOfYear = daysOfMinYearMinusOne;
} else {
dayOfYear = cal.GetDaysInYear(year - 1);
}
dayForJan1 = dayForJan1 - (dayOfYear % 7);
// Now, calucalte the offset. Substract the first day of week from the dayForJan1. And make it a positive value.
offset = (firstDayOfWeek - dayForJan1 + 14) % 7;
if (offset != 0 && offset >= fullDays)
{
//
// If the offset is greater than the value of fullDays, it means that
// the first week of the year starts on the week where Jan/1 falls on.
//
offset -= 7;
}
//
// Calculate the day of year for specified time by taking offset into account.
//
day = dayOfYear - offset;
return (day/7 + 1);
}
示例7: InternalGetWeekOfYearFullDays
internal static int InternalGetWeekOfYearFullDays(Calendar cal, DateTime time, int firstDayOfWeek, int fullDays, int daysOfMinYearMinusOne)
{
int daysInYear = cal.GetDayOfYear(time) - 1;
int num = ((int) cal.GetDayOfWeek(time)) - (daysInYear % 7);
int num2 = ((firstDayOfWeek - num) + 14) % 7;
if ((num2 != 0) && (num2 >= fullDays))
{
num2 -= 7;
}
int num3 = daysInYear - num2;
if (num3 < 0)
{
int year = cal.GetYear(time);
if (year <= cal.GetYear(cal.MinSupportedDateTime))
{
daysInYear = daysOfMinYearMinusOne;
}
else
{
daysInYear = cal.GetDaysInYear(year - 1);
}
num -= daysInYear % 7;
num2 = ((firstDayOfWeek - num) + 14) % 7;
if ((num2 != 0) && (num2 >= fullDays))
{
num2 -= 7;
}
num3 = daysInYear - num2;
}
return ((num3 / 7) + 1);
}