本文整理汇总了C#中this.AddYears方法的典型用法代码示例。如果您正苦于以下问题:C# this.AddYears方法的具体用法?C# this.AddYears怎么用?C# this.AddYears使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.AddYears方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetDayInYear
public static DateTime SetDayInYear(this DateTime @this, int day)
{
if (day > 0)
{
return @this.AddDays([email protected]).AddDays(day);
}
else
{
return @this.AddYears(1).AddDays([email protected](1).DayOfYear).AddDays(day);
}
}
示例2: AddYearsAndManageLeapYear
public static DateTime AddYearsAndManageLeapYear(this DateTime date, int value)
{
Contract.Requires(value >= 1 && value <= 9999);
int currentYear = date.Year;
int diff = value - currentYear;
bool currentYearIsLeapYear = DateTime.IsLeapYear(currentYear);
bool newYearIsLeapYear = DateTime.IsLeapYear(value);
return !currentYearIsLeapYear && newYearIsLeapYear && date.Day == 28 && date.Month == 2
? date.AddYears(diff).AddDays(1)
: date.AddYears(diff);
}
示例3: 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;
}
示例4: Ago
/// <summary>
/// Returns a new <see cref="System.DateTimeOffset" /> that minus the provided parameters (<paramref name="years" />, <paramref name="months" />, <paramref name="days" />, <paramref name="hours" />, <paramref name="minutes" />, <paramref name="seconds" />) value/s.
/// </summary>
/// <param name="source"><see cref="System.DateTimeOffset" /> instance.</param>
/// <param name="years">The number of years to reduce.</param>
/// <param name="months">The number of months to reduce.</param>
/// <param name="days">The number of days to reduce.</param>
/// <param name="hours">The number of hours to reduce.</param>
/// <param name="minutes">The number of minutes to reduce.</param>
/// <param name="seconds">The number of seconds to reduce.</param>
/// <returns>Returns a new <see cref="System.DateTimeOffset" /> that minus the provided parameters (<paramref name="years" />, <paramref name="months" />, <paramref name="days" />, <paramref name="hours" />, <paramref name="minutes" />, <paramref name="seconds" />) value/s.</returns>
public static DateTimeOffset Ago(this DateTimeOffset source, int years = 0, int months = 0, int days = 0, int hours = 0, int minutes = 0, int seconds = 0)
{
return source.AddYears(years * -1)
.AddMonths(months * -1)
.AddDays(days * -1)
.AddHours(hours * -1)
.AddMinutes(minutes * -1)
.AddSeconds(seconds * -1);
}
示例5: Advance
/// <summary>
/// Returns a new <see cref="System.DateTime" /> that adds the provided parameters (<paramref name="years" />, <paramref name="months" />, <paramref name="days" />, <paramref name="hours" />, <paramref name="minutes" />, <paramref name="seconds" />) value/s.
/// </summary>
/// <param name="source"><see cref="System.DateTime" /> instance.</param>
/// <param name="years">The number of years to add.</param>
/// <param name="months">The number of months to add.</param>
/// <param name="days">The number of days to add.</param>
/// <param name="hours">The number of hours to add.</param>
/// <param name="minutes">The number of minutes to add.</param>
/// <param name="seconds">The number of seconds to add.</param>
/// <returns>Returns a new <see cref="System.DateTime" /> that adds the provided parameters (<paramref name="years" />, <paramref name="months" />, <paramref name="days" />, <paramref name="hours" />, <paramref name="minutes" />, <paramref name="seconds" />) value/s.</returns>
public static DateTime Advance(this DateTime source, int years = 0, int months = 0, int days = 0, int hours = 0, int minutes = 0, int seconds = 0)
{
return source.AddYears(years)
.AddMonths(months)
.AddDays(days)
.AddHours(hours)
.AddMinutes(minutes)
.AddSeconds(seconds);
}
示例6: NextYear
///////////////////////////////////////////////////////////////////////
public static DateTime NextYear(this DateTime date)
{
DateTime next = date.AddYears(1);
next = next.SetMonth(1);
next = next.SetDayOfMonth(1);
next = next.SetHour(0);
next = next.SetMinute(0);
next = next.SetSecond(0);
next = next.SetMillisecond(0);
return next;
}
示例7: CalcuateAge
public static int CalcuateAge(this DateTime theDateTime)
{
// Bruteforce Age
var age = DateTime.Today.Year - theDateTime.Year;
if (theDateTime.AddYears(age) > DateTime.Today)
{
age--;
}
return age;
}
示例8: 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;
}
示例9: 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;
}
示例10: GetDifferenceInFullYearsWith
public static int GetDifferenceInFullYearsWith(this DateTime firstDate, DateTime secondDate)
{
// http://stackoverflow.com/questions/4127363/date-difference-in-years-c-sharp
// + http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c
DateTime zeroTime = new DateTime(1, 1, 1);
var timeSpan = firstDate - secondDate;
var yearResult = (zeroTime + timeSpan).Year - 1; // because we start at year 1 for the Gregorian calendar, we must subtract a year here.
// Il peut y avoir des cas à la con : 31/05/2014-01/06/1995 donne 19 ans alors qu'il nous faut 18 ans ! (probablement dû aux années bisextiles)
if (firstDate.AddYears(-yearResult) < secondDate)
{
yearResult--;
}
return yearResult;
}
示例11: 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;
}
示例12: YearsAgo
/// <summary>
/// Returns a DateTime a number of years in the past
/// </summary>
/// <param name="self">The DateTime to move</param>
/// <param name="years">Number of years</param>
public static DateTime YearsAgo(this DateTime self, int years)
{
return self.AddYears(-years);
}
示例13: GetPreviousYear
/// <summary>
/// Method for getting previous year
/// </summary>
/// <param name="date">Date</param>
/// <returns>Previous year</returns>
public static DateTime GetPreviousYear(this DateTime date)
{
return date.AddYears(-1);
}
示例14: AddCenturies
/// <summary>
/// Adds the given centuries to the given DateTime.
/// </summary>
/// <param name="date">The DateTime to which the centuries have to be added.</param>
/// <param name="centuries">The number of centuries to be added.</param>
/// <returns>The DateTime after the centuries are added.</returns>
public static DateTime AddCenturies(this DateTime date, int centuries)
{
return date.AddYears(centuries * YEARS_PER_CENTURY);
}
示例15: AddDecades
/// <summary>
/// Adds the given decades to the given DateTime.
/// </summary>
/// <param name="date">The DateTime to which the decades have to be added.</param>
/// <param name="decades">The number of decades to be added.</param>
/// <returns>The DateTime after the decades are added.</returns>
public static DateTime AddDecades(this DateTime date, int decades)
{
return date.AddYears(decades * YEARS_PER_DECADE);
}