本文整理汇总了C#中this.AddMonths方法的典型用法代码示例。如果您正苦于以下问题:C# this.AddMonths方法的具体用法?C# this.AddMonths怎么用?C# this.AddMonths使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.AddMonths方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Interval
public static DateTime Interval(this DateTime date, DateTimePart? intervalType, int? intervalVal)
{
if (intervalType.HasValue && intervalVal.HasValue)
{
switch (intervalType.Value)
{
case DateTimePart.Year:
return date.AddYears(intervalVal.Value);
case DateTimePart.Month:
return date.AddMonths(intervalVal.Value);
case DateTimePart.Day:
return date.AddDays((double)intervalVal.Value);
case DateTimePart.Hour:
return date.AddHours((double)intervalVal.Value);
case DateTimePart.Munite:
return date.AddMinutes((double)intervalVal.Value);
case DateTimePart.Second:
return date.AddSeconds((double)intervalVal.Value);
case DateTimePart.Week:
return date.AddDays((double)intervalVal.Value * 7);
case DateTimePart.Quarter:
return date.AddMonths(intervalVal.Value * 3);
}
}
return date;
}
示例2: SetDayInMonth
public static DateTime SetDayInMonth(this DateTime @this, int day)
{
if (day > 0)
{
return @this.AddDays([email protected]).AddDays(day);
}
else
{
return @this.AddMonths(1).AddDays([email protected](1).Day).AddDays(day);
}
}
示例3: AddCalendarMonths
/// <summary>
/// Returns the date after n full calendar months have elapsed, starting
/// from a given date (this).
/// </summary>
/// <remarks>
/// This only works when adding, not subtracting, calendar months.
/// </remarks>
public static DateTime AddCalendarMonths(this DateTime dt, int n)
{
// Advance n months...
int y = dt.AddMonths(n).Year;
int m = dt.AddMonths(n).Month;
// ...then get (the day after) the last day in that month
if (dt.Day != 1)
return new DateTime(y, m, DateTime.DaysInMonth(y,m)).AddDays(1);
// unless it's the first of the month (special case)
else
return dt.AddMonths(n);
}
示例4: AddInterval
/// <summary>
/// Add an interval of time (or some multiple thereof) to a DateTime.
/// </summary>
public static DateTime AddInterval(this DateTime dt, Time.IntervalType interval, int numberOfIntervals)
{
if (interval == Time.IntervalType.Day)
return dt.AddDays(numberOfIntervals);
else if (interval == Time.IntervalType.Week)
return dt.AddDays(7 * numberOfIntervals);
else if (interval == Time.IntervalType.Month)
return dt.AddMonths(numberOfIntervals);
else if (interval == Time.IntervalType.Quarter) // won't necessarily work for qtr end dates
return dt.AddMonths(3 * numberOfIntervals);
else if (interval == Time.IntervalType.Year)
return dt.AddYears(numberOfIntervals);
else
return dt;
}
示例5: AddMonths
public static DateTime AddMonths(this DateTime value, int months, bool ignoreSundays)
{
int extraDaysToAdd = 0;
while (ignoreSundays && value.AddMonths(months).AddDays(extraDaysToAdd).DayOfWeek == DayOfWeek.Sunday)
extraDaysToAdd += months < 0 ? -1 : 1;
return value.AddDays(extraDaysToAdd);
}
示例6: NextMonth
public static DateTime NextMonth(this DateTime date)
{
if (date.Day != DateTime.DaysInMonth(date.Year, date.Month))
return date.AddMonths(1);
else
return date.AddDays(1).AddMonths(1).AddDays(-1);
}
示例7: LastSevenMonths
public static IEnumerable<DateTime> LastSevenMonths(this DateTime dateTime)
{
for (var i = 1; i <= 7; i++)
{
var lastMonth = dateTime.AddMonths(1 - i);
yield return new DateTime(lastMonth.Year, lastMonth.Month, 1);
}
}
示例8: NextMonth
///////////////////////////////////////////////////////////////////////
public static DateTime NextMonth(this DateTime date)
{
DateTime next = date.AddMonths(1);
next = next.SetDayOfMonth(1);
next = next.SetHour(0);
next = next.SetMinute(0);
next = next.SetSecond(0);
next = next.SetMillisecond(0);
return next;
}
示例9: AddMonthsMatchDay
/// <summary>
/// Adds the specified number of months to a DateTime. Also sets the day of the month to match what you pass in, or
/// the max day of the month, whichever is lesser. This function is useful when iterating through months and you
/// initially start on the 31st and then in subsequent months get moved to 28 but then later want to return to the 31st.
/// </summary>
/// <param name="dt"></param>
/// <param name="numMonths"></param>
/// <param name="dayToMatch"></param>
/// <returns></returns>
public static DateTime AddMonthsMatchDay(this DateTime dt, int numMonths, int dayToMatch)
{
// Add the number of months to our original date
var newDate = dt.AddMonths(numMonths);
// Find which is less, the day to match or the number of days in the month
var lesser = NumberExtensions.LesserOf(dayToMatch, newDate.DaysInMonth());
// Add/Remove the number of days to match what was picked in the previous line as the lesser day
return newDate.AddDays(lesser - newDate.Day);
}
示例10: GetNearFirstDate
public static DateTime GetNearFirstDate(this DateTime date)
{
if (date.Day == 1)
{
return date;
}
DateTime nextDate = date.AddMonths(1);
return new DateTime(nextDate.Year, nextDate.Month, 1);
}
示例11: AddMonthsAvoidWeekend
public static DateTime AddMonthsAvoidWeekend(this DateTime dt, int months)
{
DateTime temp = dt.AddMonths(months);
if (temp.DayOfWeek == DayOfWeek.Saturday)
temp = temp.AddDays(2);
if (temp.DayOfWeek == DayOfWeek.Sunday)
temp = temp.AddDays(1);
return temp;
}
示例12: AddMonths
/// <summary>
/// Returns a new <see cref="DateTime"/> that adds the specified number of months to the value of this instance.
/// </summary>
/// <param name="dateTime">A <see cref="DateTime"/>.</param>
/// <param name="months">A number of months. The <paramref name="months"/> parameter can be negative or
/// positive.</param>
/// <returns>An object whose value is the sum of the date and time represented by this instance and <paramref
/// name="months"/>.</returns>
public static DateTime AddMonths(this DateTime dateTime, double months)
{
int wholeMonths = (int)months;
double partMonth = months % 1.0;
// Add whole months
dateTime = dateTime.AddMonths(wholeMonths);
// Add part month if required
if (partMonth > 0.0)
{
int monthInDays = (dateTime.AddMonths(1) - dateTime).Days;
dateTime = dateTime.AddDays(Math.Round(monthInDays * partMonth));
}
else if (partMonth < 0.0)
{
int monthInDays = (dateTime - dateTime.AddMonths(-1)).Days;
dateTime = dateTime.AddDays(Math.Round(monthInDays * partMonth));
}
return dateTime;
}
示例13: Add
/// <summary>
/// Adds a generic AddType to a DateTime object
/// </summary>
/// <param name="now"><seealso cref="System.DateTime"/></param>
/// <param name="adder">Type structure that acts as a switcher for what type of add to perform</param>
/// <param name="much">How much AddType to add to each element for creating list of data</param>
/// <returns>A DateTime object with the added AddType amounts</returns>
public static DateTime Add(this DateTime now, AddType adder, double much)
{
DateTime ret = now;
switch (adder)
{
case AddType.Years:
{
ret = now.AddYears((int)much);
break;
}
case AddType.Months:
{
ret = now.AddMonths((int)much);
break;
}
case AddType.Days:
{
ret = now.AddDays(much);
break;
}
case AddType.Hours:
{
ret = now.AddHours(much);
break;
}
case AddType.Minutes:
{
ret = now.AddMinutes(much);
break;
}
case AddType.Seconds:
{
ret = now.AddSeconds(much);
break;
}
case AddType.Milliseconds:
{
ret = now.AddMilliseconds(much);
break;
}
case AddType.Ticks:
{
ret = now.AddTicks((long)much);
break;
}
}
return ret;
}
示例14: GenerateMonthlyRecurrenceWithCalendarDaySpecification
public static IEnumerable<DateTime> GenerateMonthlyRecurrenceWithCalendarDaySpecification(this DateTime begin, int recurrenceInterval, DateTime repeatUntilDate)
{
yield return begin;
var targetDayOfMonth = begin.Day;
while (true)
{
begin = begin.AddMonths(recurrenceInterval);
var tempBegin = GetExistingBegin(begin, targetDayOfMonth);
begin = tempBegin;
if (begin.Date > repeatUntilDate.Date)
{
yield break;
}
yield return begin;
}
}
示例15: SubstractTimeStepUnit
public static DateTime SubstractTimeStepUnit(this DateTime dt, TimeStepUnit tstep)
{
switch (tstep)
{
case TimeStepUnit.Year:
return dt.AddYears(-1);
case TimeStepUnit.Month:
return dt.AddMonths(-1);
case TimeStepUnit.Day:
return dt.AddDays(-1);
case TimeStepUnit.Hour:
return dt.AddHours(-1);
case TimeStepUnit.Minute:
return dt.AddMinutes(-1);
case TimeStepUnit.Second:
return dt.AddSeconds(-1);
}
return dt;
}