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


C# DateTime.AddYears方法代码示例

本文整理汇总了C#中System.DateTime.AddYears方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.AddYears方法的具体用法?C# DateTime.AddYears怎么用?C# DateTime.AddYears使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.DateTime的用法示例。


在下文中一共展示了DateTime.AddYears方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddPeriodToDate

        public static DateTime AddPeriodToDate(DateTime utcDate, Periods period)
        {
            switch(period)
            {
                case Periods.FiveYears:
                    return utcDate.AddYears(5);
                case Periods.Forever:
                    return DateTime.MaxValue;
                case Periods.OneMonth:
                    return utcDate.AddMonths(1);
                case Periods.OneTime:
                    return utcDate;
                case Periods.OneWeek:
                    return utcDate.AddDays(7);
                case Periods.OneYear:
                    return utcDate.AddYears(1);
                case Periods.SixMonths:
                    return utcDate.AddMonths(6);
                case Periods.ThirtyDays:
                    return utcDate.AddDays(30);
                case Periods.ThreeMonths:
                    return utcDate.AddMonths(3);
                case Periods.TwoWeeks:
                    return utcDate.AddDays(14);
                case Periods.TwoYears:
                    return utcDate.AddYears(2);
                case Periods.Unknown:
                    return utcDate;
            }

            return utcDate;
        }
开发者ID:NightOwl888,项目名称:MerchantTribe,代码行数:32,代码来源:Service.cs

示例2: CalculatedRecurrenceSetPagingWorks

            public void CalculatedRecurrenceSetPagingWorks()
            {
                #region Arrange

                var validStartDateTime = new DateTime(2014, 2, 19);
                const int validYearsBetweenOccurrences = 2;
                DateTime validEndDateTime = validStartDateTime.AddYears(6);
                var validYearlyRecurrence = new YearlyRecurrence(validStartDateTime, validYearsBetweenOccurrences,
                    validEndDateTime);

                #endregion

                #region Act

                const int validSkip = 1;
                const int validTake = 2;
                IEnumerable<DateTime> recurrenceSet = validYearlyRecurrence.CalculateRecurrenceSet(validSkip, validTake);

                #endregion

                #region Assert

                var expectedRecurrenceSet = new List<DateTime>
                {
                    validStartDateTime.AddYears(validYearsBetweenOccurrences*1),
                    validStartDateTime.AddYears(validYearsBetweenOccurrences*2)
                };

                Assert.IsTrue(expectedRecurrenceSet.SequenceEqual(recurrenceSet));

                #endregion
            }
开发者ID:TonightWe,项目名称:TW.Recurrence,代码行数:32,代码来源:YearlyRecurrenceTests.cs

示例3: Main

        static void Main(string[] args)
        {
            var d = new DateTime(2014, 12, 31);

            WriteLine(d.ToString("d"));

            Debugger.Break();

            // years
            WriteLine(d.AddYears(2).Year);
            WriteLine(d.AddYears(1).AddYears(1).Year);

            // days
            WriteLine(d.AddDays(62).ToString("d"));
            WriteLine(d.AddDays(31).AddDays(31).ToString("d"));

            // months
            WriteLine(d.AddMonths(1).ToString("d"));

            WriteLine(d.AddMonths(2).ToString("d"));
            WriteLine(d.AddMonths(1).AddMonths(1).ToString("d"));

            WriteLine(d.AddMonths(3).ToString("d"));
            WriteLine(d.AddMonths(1).AddMonths(1).AddMonths(1).ToString("d"));

            WriteLine(d.AddMonths(4).ToString("d"));
            WriteLine(d.AddMonths(1).AddMonths(1).AddMonths(1).AddMonths(1).ToString("d"));
        }
开发者ID:modulexcite,项目名称:TenUnusualThings,代码行数:28,代码来源:Program.cs

示例4: FormatAge

        static string FormatAge(DateTime bday)
        {
            var today = DateTime.Today;
            var age = today - bday;

            if (age.Days < 7)
            {
                return string.Format("{0} {1}", age.Days, age.Days == 1 ? "dag" : "dagen");
            }
            if (today < bday.AddMonths(3))
            {
                var weeks = age.Days / 7;
                return string.Format("{0} {1}", weeks, weeks == 1 ? "week" : "weken");
            }
            if (today < bday.AddYears(1))
            {
                var months = today.Month - bday.Month < 0 ? today.Month - bday.Month + 12 : today.Month - bday.Month;
                return string.Format("{0} {1}", months, months == 1 ? "maand" : "maanden");
            }
            if (today < bday.AddYears(2))
            {
                var months = today.Month - bday.AddYears(1).Month < 0 ? today.Month - bday.AddYears(1).Month + 12 : today.Month - bday.AddYears(1).Month;

                if (months == 0)
                    return "1 jaar";

                return string.Format("1 jaar en {0} {1}", months, months == 1 ? "maand" : "maanden");
            }
            return string.Format("{0} jaar", today.Year - bday.Year);
        }
开发者ID:PeterVDD,项目名称:Kermit,代码行数:30,代码来源:AgeConverter.cs

示例5: CalculateSeries

 protected override void CalculateSeries(int firstIndex, int lastIndex)
 {
   if (firstIndex > lastIndex)
     return;
   DateTime dateTime1 = this.parentSeries.GetDateTime(firstIndex);
   DateTime dateTime2 = this.parentSeries.GetDateTime(lastIndex);
   for (DateTime DateTime = new DateTime(dateTime1.Year, 1, 1); DateTime <= dateTime2; DateTime = DateTime.AddYears(1))
     this.series.Add(DateTime, this.parentSeries[DateTime.AddYears(1).AddTicks(-1L), EIndexOption.Prev]);
 }
开发者ID:heber,项目名称:FreeOQ,代码行数:9,代码来源:SimpleAnnualSeries.cs

示例6: AgeTest

        public void AgeTest()
        {
            DateTime birthday = new DateTime(1980, 1, 1);

            Assert.AreEqual(0,DateHelper.Age(birthday,birthday)); // Same Day
            Assert.AreEqual(1, DateHelper.Age(birthday, birthday.AddYears(1))); // One Year
            Assert.AreEqual(0,DateHelper.Age(birthday,birthday.AddYears(1).AddDays(-1))); // One day less than a year
            Assert.AreEqual(10,DateHelper.Age(birthday,birthday.AddYears(10).AddMonths(1).AddDays(3))); // 10 year, 1 month , 3 dys
        }
开发者ID:robjefferies,项目名称:tacklebox,代码行数:9,代码来源:DateHelperTest.cs

示例7: TestDayExclusionDifferentYears

 public void TestDayExclusionDifferentYears()
 {
     string errMessage = "Day was not excluded when it was supposed to be excluded";
     DateTime d = new DateTime(2005, 1, 1);
     cal.SetDayExcluded(d, true);
     Assert.IsTrue(cal.IsDayExcluded(d), errMessage);
     Assert.IsTrue(cal.IsDayExcluded(d.AddYears(-2)), errMessage);
     Assert.IsTrue(cal.IsDayExcluded(d.AddYears(2)), errMessage);
     Assert.IsTrue(cal.IsDayExcluded(d.AddYears(100)), errMessage);
 }
开发者ID:CharlieBP,项目名称:quartznet,代码行数:10,代码来源:AnnualCalendarTest.cs

示例8: ValidRelativeMonthYear

        internal static bool ValidRelativeMonthYear(MonthYear input,
                                                    int lowerBound, MonthYearUnit lowerUnit, RangeBoundaryType lowerBoundType,
                                                    int upperBound, MonthYearUnit upperUnit, RangeBoundaryType upperBoundType)
        {
            DateTime now = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime lowerDate = DateTime.MinValue;
            DateTime upperDate = DateTime.MaxValue;
            DateTime inputDate = new DateTime(input.Year, input.Month, 1);

            switch (lowerUnit)
            {
                case MonthYearUnit.Month:
                    lowerDate = now.AddMonths(lowerBound * -1);
                    break;
                case MonthYearUnit.Year:
                    lowerDate = now.AddYears(lowerBound * -1);
                    break;
            }
            switch (upperUnit)
            {
                case MonthYearUnit.Month:
                    upperDate = now.AddMonths(upperBound);
                    break;
                case MonthYearUnit.Year:
                    upperDate = now.AddYears(upperBound);
                    break;
            }

            //default the bound check to true - if lowerBoundType is Ignore, no test will be performed.
            bool lowerBoundOk = true;
            if (lowerBoundType == RangeBoundaryType.Inclusive)
            {
                lowerBoundOk = inputDate.CompareTo(lowerDate) >= 0;
            }
            else if (lowerBoundType == RangeBoundaryType.Exclusive)
            {
                lowerBoundOk = inputDate.CompareTo(lowerDate) > 0;
            }

            //default the bound check to true - if upperBoundType is Ignore, no test will be performed.
            bool upperBoundOk = true;
            if (upperBoundType == RangeBoundaryType.Inclusive)
            {
                upperBoundOk = inputDate.CompareTo(upperDate) <= 0;
            }
            else if (upperBoundType == RangeBoundaryType.Exclusive)
            {
                upperBoundOk = inputDate.CompareTo(upperDate) < 0;
            }

            return lowerBoundOk && upperBoundOk;
        }
开发者ID:GrahamClark,项目名称:TestConsoleApp,代码行数:52,代码来源:Runner.cs

示例9: StringToDateTime

		public StringToDateTime(string dtstr) {
		//TODO: 曜日。月末。日にちを25thというように指定できる。
		/*
			Su(nday)	曜日。先頭の2文字。小文字でも可能。年月の指定と同時にできない。
			[12]?(1st|2nd|3rd|\dth)	日にち。年月の指定と同時にできない。
			5/$		月末。パースする前に28日に置換される。月日が確定したらその月の月末に移動する。
			+/-計算||月末記号を置換→普通にパース→月末に移動→<>@を反映
		*/
			DateTime       dt  = new DateTime ();
			CultureInfo    ci  = new CultureInfo("", true);
			DateTimeStyles dts = (DateTimeStyles.AllowTrailingWhite
			                   |  DateTimeStyles.NoCurrentDateDefault);
			try {
				// ((yy)yy/)MM/dd、12:34(:56)、およびその組み合わせ、が成功する。年が指定されていない場合は今年。日にちが指定されていない場合は西暦1年元旦。
				dt = DateTime.Parse(dtstr, ci, dts);

			} catch (FormatException) {
				dt = ParseString(dtstr, ci, dts);
			} finally {
				// 現時点に最も近い日時に変更する。
				DateTime today = DateTime.Today;
				if(dt.Year < 1000) {
					if(dt.Month == 1) {
						if(dt.Day == 1) {
							dt = today.Date + dt.TimeOfDay;
						} else {
							dt = dt.AddYears((dt.Year == 1 ? today.Year - 1 : 2000)).AddMonths(today.Month - 1);
						}
					} else {
						dt = dt.AddYears((dt.Year == 1 ? today.Year - 1 : 2000));
					}
				} else {
					if(dt.Month == 1 && today.Month != 1) {
						dt = dt.AddMonths(today.Month - 1);
					}
				}
				/*
				>日時		直後の。現時点より新しい日時
				<日時		直前の。現時点より古い日時
				@日時		直近の。現時点より新しい日時と古い日時とを比較して、時間的に近い方の日時
				+日/時	現時点から指定された時間を加算する。+1の代わりに+でも可能
				-日/時	現時点から指定された時間を減算する。-1の代わりに-でも可能
				例:
				+1 15:	翌日の15時。
				+1 +15:	翌日の同時刻から15時間後。
				+1 @15:	翌日の同時刻から最も近い15時。
				+sun
				*/
			}
			this.dateTime = dt;
		}
开发者ID:taketori,项目名称:d.hatena.mac,代码行数:51,代码来源:x10d.HidemaruMacro.Date2String.cs

示例10: Download

        static void Download()
        {
            var from = new DateTime(2005, 4, 2);
            var to = new DateTime(2016, 4, 2);

            //multithreaded runner to download data faster...
            for (int i = 0; i < 11; i++)
            {
                var f = i == 0 ? from : from.AddYears(i);
                var t = i == 10 ? to : from.AddYears(i + 1).AddDays(-1);

                //var task = new Task(() => Run(f, t));
                //task.Start();
            }
        }
开发者ID:af3290,项目名称:ema,代码行数:15,代码来源:Program.cs

示例11: GetAge

 public int GetAge(DateTime now)
 {
     var age = now.Year - Dob.Year;
     if (now.AddYears(-1 * age) < Dob)
         age--;
     return age;
 }
开发者ID:robdmoore,项目名称:TestFixtureDataGenerationPresentation,代码行数:7,代码来源:Member.cs

示例12: Add

 public static DateTime Add(DatePartType datePart, long add, DateTime dt)
 {
     switch (datePart) {
     case DatePartType.Year:
         return dt.AddYears ((int)add);
     case DatePartType.Quarter:
         return dt.AddMonths ((int)add * 3);
     case DatePartType.Month:
         return dt.AddMonths ((int)add);
     case DatePartType.Day:
     case DatePartType.DayOfYear:
     case DatePartType.DayOfWeek:
         return dt.AddDays (add);
     case DatePartType.Hour:
         return dt.AddHours (add);
     case DatePartType.Minute:
         return dt.AddMinutes (add);
     case DatePartType.Second:
         return dt.AddSeconds (add);
     case DatePartType.MilliSecond:
         return dt.AddMilliseconds (add);
     case DatePartType.MicroSecond:
         return dt.AddTicks (add * TimeSpan.TicksPerMillisecond / 1000);
     case DatePartType.NanoSecond:
         return dt.AddTicks (add * TimeSpan.TicksPerMillisecond / 1000000);
     default:
         throw new InvalidOperationException (string.Format ("Unknown DatePart type '{0}'", datePart));
     }
 }
开发者ID:WimObiwan,项目名称:FxGqlC,代码行数:29,代码来源:DateFunctionsHelper.cs

示例13: IncrementDate

        public static DateTime IncrementDate(this TimePeriod period, DateTime date, int amount)
        {
            switch (period) {
                case TimePeriod.Millisecond:
                    return date.AddMilliseconds(amount);
                case TimePeriod.Second:
                    return date.AddSeconds(amount);
                case TimePeriod.Minute:
                    return date.AddMinutes(amount);
                case TimePeriod.Hour:
                    return date.AddHours(amount);
                case TimePeriod.Day:
                    return date.AddDays(amount);
                case TimePeriod.Month:
                    return date.AddMonths(amount);
                case TimePeriod.Quarter:
                    return date.AddMonths(amount * 3);
                case TimePeriod.Half:
                    return date.AddMonths(amount * 6);
                case TimePeriod.Year:
                    return date.AddYears(amount);

                default:
                    throw new Exception("Unknown period: " + period.ToString());

            }
        }
开发者ID:GrowingData,项目名称:Mung,代码行数:27,代码来源:TimePeriod.cs

示例14: nextAnniversary

 public static DateTime nextAnniversary(this DateTime dateTime)
 {
     DateTime nextTime = new DateTime(DateTime.Now.Year, dateTime.Month, dateTime.Day);
     if (nextTime.CompareTo(DateTime.Now.cropToDays()) < 0)
         return nextTime.AddYears(1);
     return nextTime;
 }
开发者ID:Eluch,项目名称:Date-Reminder,代码行数:7,代码来源:Extensions.cs

示例15: DateRange

 public DateRange(DateTime CurrentDate,DateRangeFilter filter )
 {
     if (filter == DateRangeFilter.ThisMonth)
     {
         StartDate = GetFirstDayOfMonth(CurrentDate);
         EndDate = GetLastDayOfMonth(CurrentDate);
     }
     else if (filter == DateRangeFilter.ThisYear)
     {
         StartDate = GetFirstDayOfYear(CurrentDate);
         EndDate = GetLastDayOfYear(CurrentDate);
     }
     else if (filter == DateRangeFilter.Last7Days)
     {
         EndDate = CurrentDate;
         StartDate = EndDate.AddDays(-7);
     }
     else if (filter == DateRangeFilter.Last3Month)
     {
         EndDate = CurrentDate;
         StartDate = EndDate.AddMonths(-3);
     }
     else if (filter == DateRangeFilter.Last365Days)
     {
         EndDate = CurrentDate;
         StartDate = EndDate.AddYears(-1);
     }
     else if (filter == DateRangeFilter.AnyTime)
     {
         StartDate = DateTime.MinValue;
         EndDate = GetLastDayOfYear(CurrentDate);
     }
 }
开发者ID:tuanzhang7,项目名称:acct.aspnet,代码行数:33,代码来源:DateRange.cs


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