当前位置: 首页>>代码示例>>C#>>正文


C# this.AddYears方法代码示例

本文整理汇总了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);
     }
 }
开发者ID:mattiasnordqvist,项目名称:Golden-Fox,代码行数:11,代码来源:DateTimeExtensions.cs

示例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);
        }
开发者ID:nbouilhol,项目名称:bouilhol-lib,代码行数:13,代码来源:DateTimeExtension.cs

示例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;
 }
开发者ID:cairabbit,项目名称:daf,代码行数:26,代码来源:DateTimeExtensions.cs

示例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);
 }
开发者ID:jittuu,项目名称:NSupport,代码行数:20,代码来源:DateTimeOffsetCalculations.cs

示例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);
 }
开发者ID:jittuu,项目名称:NSupport,代码行数:20,代码来源:DateTimeCalculations.cs

示例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;
 }
开发者ID:jheddings,项目名称:flynn,代码行数:12,代码来源:DateTimeFu.cs

示例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;
    }
开发者ID:jimxshaw,项目名称:samples-csharp,代码行数:11,代码来源:DateTimeExtensions.cs

示例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;
 }
开发者ID:mpoulshock,项目名称:hammurabi,代码行数:18,代码来源:DateTime+extension+methods.cs

示例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;
 }
开发者ID:kLeZ,项目名称:Gecko,代码行数:55,代码来源:Extensions.cs

示例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;
        }
开发者ID:LuccaSA,项目名称:NExtends,代码行数:16,代码来源:DateTime.extensions.cs

示例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;
 }
开发者ID:XiBeichuan,项目名称:hydronumerics,代码行数:19,代码来源:Extensions.cs

示例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);
 }
开发者ID:ramonsmits,项目名称:Umbrella,代码行数:9,代码来源:DateTimeCalendarExtensions.cs

示例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);
 }
开发者ID:Rei-li,项目名称:Dixiton,代码行数:9,代码来源:DateHelper.cs

示例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);
 }
开发者ID:rocketgames,项目名称:Extension-Methods-for-Unity,代码行数:10,代码来源:DateTimeExtensions.cs

示例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);
 }
开发者ID:rocketgames,项目名称:Extension-Methods-for-Unity,代码行数:10,代码来源:DateTimeExtensions.cs


注:本文中的this.AddYears方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。