本文整理汇总了C#中System.Globalization.Calendar.AddDays方法的典型用法代码示例。如果您正苦于以下问题:C# Calendar.AddDays方法的具体用法?C# Calendar.AddDays怎么用?C# Calendar.AddDays使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Globalization.Calendar
的用法示例。
在下文中一共展示了Calendar.AddDays方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetEraRanges
/// <summary>
/// Builds an array of <see cref="MonthCalendarEraRange"/> to store the min and max date of the eras of the specified
/// <see cref="System.Globalization.Calendar"/>.
/// </summary>
/// <param name="cal">
/// The <see cref="System.Globalization.Calendar"/> to retrieve the era ranges for.
/// </param>
/// <returns>
/// An array of type <see cref="MonthCalendarEraRange"/>.
/// </returns>
private static MonthCalendarEraRange[] GetEraRanges(Calendar cal)
{
if (cal.Eras.Length == 1)
{
return new[] { new MonthCalendarEraRange(cal.Eras[0], cal.MinSupportedDateTime.Date, cal.MaxSupportedDateTime.Date) };
}
List<MonthCalendarEraRange> ranges = new List<MonthCalendarEraRange>();
DateTime date = cal.MinSupportedDateTime.Date;
int currentEra = -1;
while (date < cal.MaxSupportedDateTime.Date)
{
int era = cal.GetEra(date);
if (era != currentEra)
{
ranges.Add(new MonthCalendarEraRange(era, date));
if (currentEra != -1)
{
ranges[ranges.Count - 2].MaxDate = cal.AddDays(date, -1);
}
currentEra = era;
}
date = cal.AddDays(date, 1);
}
ranges[ranges.Count - 1].MaxDate = date;
return ranges.ToArray();
}
示例2: IsValidYear
/// <summary>
/// Determines if the specified <paramref name="year"/> is a valid year value.
/// </summary>
/// <param name="year">The year value.</param>
/// <param name="cal">The calendar to use.</param>
/// <param name="era">The era the year belongs to.</param>
/// <returns>true if it's a valid year value; false otherwise.</returns>
private static bool IsValidYear(int year, Calendar cal, int era)
{
int minYear = cal.GetYear(cal.MinSupportedDateTime.Date);
int maxYear = cal.GetYear(cal.MaxSupportedDateTime.Date);
if (cal.Eras.Length > 1)
{
DateTime? minDate = null, maxDate = null;
DateTime date = cal.MinSupportedDateTime;
while (date < cal.MaxSupportedDateTime.Date)
{
int e = cal.GetEra(date);
if (e == era)
{
if (minDate == null)
{
minDate = date;
}
maxDate = date;
}
date = cal.AddDays(date, 1);
}
minYear = cal.GetYear(minDate.GetValueOrDefault(cal.MinSupportedDateTime.Date));
maxYear = cal.GetYear(maxDate.GetValueOrDefault(cal.MaxSupportedDateTime.Date));
}
year = cal.ToFourDigitYear(year);
return year >= minYear && year <= maxYear;
}
示例3: WeekListEventHandler
private void WeekListEventHandler(Object source, ListCommandEventArgs e)
{
// Get the first calendar day and adjust it to the week the user
// selected (to be consistent with the index setting in Render())
_threadCalendar = DateTimeFormatInfo.CurrentInfo.Calendar;
DateTime tempDate = FirstCalendarDay(Control.VisibleDate);
Control.VisibleDate = _threadCalendar.AddDays(tempDate, e.ListItem.Index * 7);
if (_chooseOption == WeekOption)
{
// Add the whole week to the date list
DateTime endDate = _threadCalendar.AddDays(Control.VisibleDate, 6);
SelectRange(Control.VisibleDate, endDate);
HandlePostBackEvent(Done.ToString(CultureInfo.InvariantCulture));
}
else
{
SecondaryUIMode = ChooseDay;
}
}
示例4: DayListEventHandler
private void DayListEventHandler(Object source, ListCommandEventArgs e)
{
_threadCalendar = DateTimeFormatInfo.CurrentInfo.Calendar;
// VisibleDate should have been set with the first day of the week
// so the selected index can be used to adjust to the selected day.
Control.VisibleDate = _threadCalendar.AddDays(Control.VisibleDate, e.ListItem.Index);
SelectRange(Control.VisibleDate, Control.VisibleDate);
HandlePostBackEvent(Done.ToString(CultureInfo.InvariantCulture));
}
示例5: MonthListEventHandler
private void MonthListEventHandler(Object source, ListCommandEventArgs e)
{
_threadCalendar = DateTimeFormatInfo.CurrentInfo.Calendar;
if (e.ListItem.Index == _monthsToDisplay)
{
// Next was selected
Control.VisibleDate = _threadCalendar.AddMonths(
Control.VisibleDate, _monthsToDisplay);
SecondaryUIMode = ChooseMonth;
}
else if (e.ListItem.Index == _monthsToDisplay + 1)
{
// Prev was selected
Control.VisibleDate = _threadCalendar.AddMonths(
Control.VisibleDate, -_monthsToDisplay);
SecondaryUIMode = ChooseMonth;
}
else
{
// A month was selected
Control.VisibleDate = _threadCalendar.AddMonths(
Control.VisibleDate,
e.ListItem.Index);
if (_chooseOption == MonthOption)
{
// Add the whole month to the date list
DateTime beginDate = EffectiveVisibleDate(Control.VisibleDate);
Control.VisibleDate = beginDate;
DateTime endDate = _threadCalendar.AddMonths(beginDate, 1);
endDate = _threadCalendar.AddDays(endDate, -1);
SelectRange(beginDate, endDate);
HandlePostBackEvent(Done.ToString(CultureInfo.InvariantCulture));
}
else
{
SecondaryUIMode = ChooseWeek;
}
}
}
示例6: Render
//.........这里部分代码省略.........
tempDate = _threadCalendar.AddMonths(tempDate, 1);
}
arr.Add(GetDefaultLabel(NextLabel));
arr.Add(GetDefaultLabel(PreviousLabel));
DataBindAndRender(writer, _monthList, arr);
break;
// Based on the month selected in case ChooseMonth above, render a list of
// availabe weeks of the month.
case ChooseWeek:
String monthFormat = (GetNumericDateFormat()[0] == 'y') ? "yyyy/M" : "M/yyyy";
writer.Write(SR.GetString(SR.CalendarAdapterOptionChooseWeek));
writer.Write(" (");
writer.Write(Control.VisibleDate.ToString(monthFormat, CultureInfo.CurrentCulture));
writer.Write("):");
writer.WriteBreak();
// List weeks of days of the selected month. May include
// days from the previous and the next month to fill out
// all six week choices. This is consistent with the
// ASP.NET Calendar control.
// Note that the event handling code of this list control
// should be implemented according to the index content
// generated here.
tempDate = FirstCalendarDay(Control.VisibleDate);
arr = new ArrayList();
String weekDisplay;
for (int i = 0; i < 6; i++)
{
weekDisplay = tempDate.ToString(abbreviatedMonthDayPattern, CultureInfo.CurrentCulture);
weekDisplay += DaySeparator;
tempDate = _threadCalendar.AddDays(tempDate, 6);
weekDisplay += tempDate.ToString(abbreviatedMonthDayPattern, CultureInfo.CurrentCulture);
arr.Add(weekDisplay);
tempDate = _threadCalendar.AddDays(tempDate, 1);
}
DataBindAndRender(writer, _weekList, arr);
break;
// Based on the month and week selected in case ChooseMonth and ChooseWeek above,
// render a list of the dates in the week.
case ChooseDay:
writer.Write(SR.GetString(SR.CalendarAdapterOptionChooseDate));
writer.Write(":");
writer.WriteBreak();
tempDate = Control.VisibleDate;
arr = new ArrayList();
String date;
String dayName;
StringBuilder dayDisplay = new StringBuilder();
bool dayNameFirst = (GetNumericDateFormat()[0] != 'y');
for (int i = 0; i < 7; i++)
{
date = tempDate.ToString(abbreviatedMonthDayPattern, CultureInfo.CurrentCulture);
if (Control.ShowDayHeader)
{
// Use the short format for displaying day name
dayName = GetAbbreviatedDayName(tempDate);
dayDisplay.Length = 0;
if (dayNameFirst)
{
dayDisplay.Append(dayName);
dayDisplay.Append(Space);
dayDisplay.Append(date);
}
else
{
dayDisplay.Append(date);
dayDisplay.Append(Space);
dayDisplay.Append(dayName);
}
arr.Add(dayDisplay.ToString());
}
else
{
arr.Add(date);
}
tempDate = _threadCalendar.AddDays(tempDate, 1);
}
DataBindAndRender(writer, _dayList, arr);
break;
default:
Debug.Assert(false, "Unexpected Secondary UI Mode");
break;
}
writer.ExitStyle(Style, breakAfter);
}
示例7: DeferOccurrenceFallingOnWeekend
private static DateTime DeferOccurrenceFallingOnWeekend(Calendar calendar, DateTime occurrence, bool skipWeekends = true)
{
if(skipWeekends)
{
if (occurrence.DayOfWeek == DayOfWeek.Saturday)
{
occurrence = calendar.AddDays(occurrence, 2);
}
if (occurrence.DayOfWeek == DayOfWeek.Sunday)
{
occurrence = calendar.AddDays(occurrence, 1);
}
}
return occurrence.ToUniversalTime();
}
示例8: GetOccurrences
private static IEnumerable<DateTime> GetOccurrences(DateInterval interval, DatePeriod period, Calendar calendar, DateTime start, DateTime end, bool skipWeekends = true)
{
var difference = DateSpan.GetDifference(interval, start, end)/period.Quantifier;
if (start.Kind == DateTimeKind.Utc)
{
start = start.ToLocalTime();
}
for (var i = 0; i < difference; i++)
{
switch (period.Frequency)
{
case DatePeriodFrequency.Seconds:
var seconds = calendar.AddSeconds(start, period.Quantifier * i);
yield return DeferOccurrenceFallingOnWeekend(calendar, seconds, skipWeekends);
break;
case DatePeriodFrequency.Minutes:
var minutes = calendar.AddMinutes(start, period.Quantifier * i);
yield return DeferOccurrenceFallingOnWeekend(calendar, minutes, skipWeekends);
break;
case DatePeriodFrequency.Hours:
var hours = calendar.AddHours(start, period.Quantifier * i);
yield return DeferOccurrenceFallingOnWeekend(calendar, hours, skipWeekends);
break;
case DatePeriodFrequency.Days:
var days = calendar.AddDays(start, period.Quantifier*i);
yield return DeferOccurrenceFallingOnWeekend(calendar, days, skipWeekends);
break;
case DatePeriodFrequency.Weeks:
var weeks = calendar.AddWeeks(start, period.Quantifier*i);
yield return DeferOccurrenceFallingOnWeekend(calendar, weeks, skipWeekends);
break;
case DatePeriodFrequency.Months:
var months = calendar.AddMonths(start, period.Quantifier*i);
yield return DeferOccurrenceFallingOnWeekend(calendar, months, skipWeekends);
break;
case DatePeriodFrequency.Years:
var years = calendar.AddYears(start, period.Quantifier*i);
yield return DeferOccurrenceFallingOnWeekend(calendar, years, skipWeekends);
break;
default:
throw new ArgumentException("Frequency");
}
}
}
示例9: 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;
}