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


C# DateTimeOffset.AddHours方法代码示例

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


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

示例1: InitCustomers

        private static void InitCustomers()
        {
            DateTimeOffset dto = new DateTimeOffset(2015, 1, 1, 1, 2, 3, 4, TimeSpan.Zero);
            _customers = Enumerable.Range(1, 5).Select(e =>
                new DCustomer
                {
                    Id = e,
                    DateTime = dto.AddYears(e).DateTime,
                    Offset = e % 2 == 0 ? dto.AddMonths(e) : dto.AddDays(e).AddMilliseconds(10),
                    Date = e % 2 == 0 ? dto.AddDays(e).Date : dto.AddDays(-e).Date,
                    TimeOfDay = e % 3 == 0 ? dto.AddHours(e).TimeOfDay : dto.AddHours(-e).AddMilliseconds(10).TimeOfDay,

                    NullableDateTime = e % 2 == 0 ? (DateTime?)null : dto.AddYears(e).DateTime,
                    NullableOffset = e % 3 == 0 ? (DateTimeOffset?)null : dto.AddMonths(e),
                    NullableDate = e % 2 == 0 ? (Date?)null : dto.AddDays(e).Date,
                    NullableTimeOfDay = e % 3 == 0 ? (TimeOfDay?)null : dto.AddHours(e).TimeOfDay,

                    DateTimes = new [] { dto.AddYears(e).DateTime, dto.AddMonths(e).DateTime },
                    Offsets = new [] { dto.AddMonths(e), dto.AddDays(e) },
                    Dates = new [] { (Date)dto.AddYears(e).Date, (Date)dto.AddMonths(e).Date },
                    TimeOfDays = new [] { (TimeOfDay)dto.AddHours(e).TimeOfDay, (TimeOfDay)dto.AddMinutes(e).TimeOfDay },

                    NullableDateTimes = new [] { dto.AddYears(e).DateTime, (DateTime?)null, dto.AddMonths(e).DateTime },
                    NullableOffsets = new [] { dto.AddMonths(e), (DateTimeOffset?)null, dto.AddDays(e) },
                    NullableDates = new [] { (Date)dto.AddYears(e).Date, (Date?)null, (Date)dto.AddMonths(e).Date },
                    NullableTimeOfDays = new [] { (TimeOfDay)dto.AddHours(e).TimeOfDay, (TimeOfDay?)null, (TimeOfDay)dto.AddMinutes(e).TimeOfDay },

                }).ToList();
        }
开发者ID:shailendra9,项目名称:WebApi,代码行数:29,代码来源:DateAndTimeOfDayController.cs

示例2: Between

        public void Between()
        {
            var date = new DateTimeOffset(2015, 8, 7, 13, 26, 30, TimeSpan.Zero);

            Assert.IsTrue(date.Between(date.AddMonths(-3), date.AddMonths(3)));
            Assert.IsTrue(date.AddHours(36).Between(date.AddMonths(-3), date.AddMonths(3)));
            Assert.IsFalse(date.AddMonths(6).Between(date.AddMonths(-3), date.AddMonths(3)));
            Assert.IsFalse(date.AddHours(-2).Between(date, date.AddMonths(3)));

            Assert.IsTrue(date.Between(date.AddMonths(-3), date.AddMonths(3), false));
            Assert.IsTrue(date.AddHours(36).Between(date.AddMonths(-3), date.AddMonths(3), false));
            Assert.IsFalse(date.AddMonths(6).Between(date.AddMonths(-3), date.AddMonths(3), false));
            Assert.IsTrue(date.AddHours(-2).Between(date, date.AddMonths(3), false));
            Assert.IsFalse(date.AddHours(-24).Between(date, date.AddMonths(3), false));
        }
开发者ID:dterracino,项目名称:cadru,代码行数:15,代码来源:DateTimeOffsetExtensionsTests.cs

示例3: GetNextOccurrence

		internal long GetNextOccurrence()
		{
			DateTimeOffset now = _ClockSource.Now;
			DateTimeOffset next;

			switch (this.Periodicity)
			{
				case PeriodicityType.Hourly:
					next = new DateTimeOffset(now.Year, now.Month, now.Day, now.Hour, this.MinuteOffset, 0, now.Offset);

					if (next <= now)
					{
						next = next.AddHours(1.0);
					}

					break;

				case PeriodicityType.Daily:
					next = new DateTimeOffset(now.Year, now.Month, now.Day, this.HourOffset, this.MinuteOffset, 0, now.Offset);

					if (next <= now)
					{
						next = next.AddDays(1.0);
					}

					break;

				default:
					goto case PeriodicityType.Hourly;
			}

			return next.ToUniversalTime().Ticks;
		}
开发者ID:Philo,项目名称:Revalee,代码行数:33,代码来源:ConfiguredTask.cs

示例4: IsToday

        /// <summary>
        /// Returns a value indicating whether the provided DaysOfWeek value is applicable today.
        /// </summary>
        public static bool IsToday(DaysOfWeek days, DateTimeOffset currentTime)
        {
            // special handling for Night Owl so that its schedule is visible after midnight
            // i.e., if it's 2AM on Sunday, we still consider "today" to be Saturday.
            var time = (days == DaysOfWeek.NightOwl)
                ? currentTime.AddHours(-4)
                : currentTime;

            return (ToDaysOfWeek(time.DayOfWeek) & days) != DaysOfWeek.None;
        }
开发者ID:RikkiGibson,项目名称:Corvallis-Bus-Server,代码行数:13,代码来源:DaysOfWeek.cs

示例5: CanAddHoursAcrossDstTransition

        public void CanAddHoursAcrossDstTransition()
        {
            var tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            var dto = new DateTimeOffset(2015, 3, 8, 1, 0, 0, TimeSpan.FromHours(-8));
            var result = dto.AddHours(1, tz);

            var expected = new DateTimeOffset(2015, 3, 8, 3, 0, 0, TimeSpan.FromHours(-7));
            Assert.Equal(expected, result);
            Assert.Equal(expected.Offset, result.Offset);
        }
开发者ID:kronic,项目名称:corefxlab,代码行数:10,代码来源:DateTimeOffsetTests.cs

示例6: Dividing

 public void Dividing()
 {
     var now = new DateTimeOffset(2013, 8, 13, 13, 0, 0, TimeSpan.FromHours(12));
     var availableTime = new TimeRange(now, now.AddHours(1));
     var freeSlots = availableTime
         .Divide(TimeSpan.FromMinutes(15))
         .ToArray();
     Assert.AreEqual(4, freeSlots.Length);
     Assert.IsTrue(freeSlots.All(s => s.Duration == TimeSpan.FromMinutes(15)));
 }
开发者ID:richardschneider,项目名称:sepia,代码行数:10,代码来源:TimeRangeTest.cs

示例7: Main

        static void Main(string[] args)
        {
            //var offset = DateTimeOffset.Now;
            //Debug.WriteLine(offset);

            //Debug.WriteLine(DateTimeOffset.UtcNow);

            //String s = "10/1/2014 6:17 PM";
            //DateTime dt = DateTime.ParseExact(s, "MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture);
            //DateTimeOffset dto = new DateTimeOffset(dt);
            DateTimeOffset dto1 = new DateTimeOffset();

            dto1 = DateTimeOffset.Now;
            dto1 = dto1.AddHours(2);
            dto1 = dto1.AddMinutes(30);

            Debug.WriteLine(DateTimeOffset.Now);
            Debug.WriteLine(dto1);

            TimeSpan diff = dto1.Subtract(DateTimeOffset.Now);
            Debug.WriteLine(diff);
            Debug.WriteLine(diff.TotalHours);
            Debug.WriteLine(diff.TotalSeconds);
            var diffsec = (DateTimeOffset.Now - dto1).TotalSeconds;
            Debug.WriteLine(diffsec);

            //dtoff.Value.ToLocalTime().ToString("dddd, MMM dd yyyy HH:mm", new CultureInfo("en-US"));

            //Debug.WriteLine(dto1);
            //Debug.WriteLine(dto2);
            //Debug.WriteLine(DateTimeOffset.Now);
            //Debug.WriteLine(dto1 - DateTimeOffset.Now);
            //Debug.WriteLine(DateTimeOffset.Now - dto1);
            //Debug.WriteLine(dto1.CompareTo(DateTimeOffset.Now));
            //Debug.WriteLine(DateTimeOffset.Now.CompareTo(dto1));
            //Debug.WriteLine("  ");

            //Debug.WriteLine(dto1.Hour);

            double temp = -26;
            double res = 0;
            for (int i = 0; i < 40; i++)
            {
                temp++;
                res = 0.5 - (temp * 0.1);
                Debug.WriteLine("Temp: " + temp + "  Result: " + res);
            }

            //Debug.WriteLine(s);
            //Debug.WriteLine(dt.ToString());
            //Debug.WriteLine(dt.ToUniversalTime().ToString());
            //Debug.WriteLine(dto.ToString());
        }
开发者ID:nsillvan,项目名称:SNMPDemo,代码行数:53,代码来源:Program.cs

示例8: Time

 public void Time()
 {
     var date = new DateTimeOffset(year: 2016, month: 4, day: 1, hour: 1, minute: 0, second: 0, offset: TimeSpan.Zero);
     EqualityUnit
         .Create(new DateTimeKey(date, DateTimeKeyFlags.Time))
         .WithEqualValues(
             new DateTimeKey(date, DateTimeKeyFlags.Time),
             new DateTimeKey(date.AddDays(1), DateTimeKeyFlags.Time))
         .WithNotEqualValues(
             new DateTimeKey(date, DateTimeKeyFlags.Date),
             new DateTimeKey(date.AddHours(1), DateTimeKeyFlags.Time))
         .RunAll(
             (x, y) => x == y,
             (x, y) => x != y);
 }
开发者ID:jaredpar,项目名称:jenkins,代码行数:15,代码来源:DateTimeKeyTests.cs

示例9: TestAddition

    public static void TestAddition()
    {
        DateTimeOffset dt = new DateTimeOffset(new DateTime(1986, 8, 15, 10, 20, 5, 70));
        Assert.Equal(17, dt.AddDays(2).Day);
        Assert.Equal(13, dt.AddDays(-2).Day);

        Assert.Equal(10, dt.AddMonths(2).Month);
        Assert.Equal(6, dt.AddMonths(-2).Month);

        Assert.Equal(1996, dt.AddYears(10).Year);
        Assert.Equal(1976, dt.AddYears(-10).Year);

        Assert.Equal(13, dt.AddHours(3).Hour);
        Assert.Equal(7, dt.AddHours(-3).Hour);

        Assert.Equal(25, dt.AddMinutes(5).Minute);
        Assert.Equal(15, dt.AddMinutes(-5).Minute);

        Assert.Equal(35, dt.AddSeconds(30).Second);
        Assert.Equal(2, dt.AddSeconds(-3).Second);

        Assert.Equal(80, dt.AddMilliseconds(10).Millisecond);
        Assert.Equal(60, dt.AddMilliseconds(-10).Millisecond);
    }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:24,代码来源:DateTimeOffset.cs

示例10: From_FromFixedDateTime_Tests

    public void From_FromFixedDateTime_Tests(int value)
    {
        var originalPointInTime = new DateTimeOffset(1976, 12, 31, 17, 0, 0, 0, TimeSpan.Zero);

        Assert.AreEqual(value.Years().From(originalPointInTime), originalPointInTime.AddYears(value));
        Assert.AreEqual(value.Months().From(originalPointInTime), originalPointInTime.AddMonths(value));
        Assert.AreEqual(value.Weeks().From(originalPointInTime), originalPointInTime.AddDays(value*DaysPerWeek));
        Assert.AreEqual(value.Days().From(originalPointInTime), originalPointInTime.AddDays(value));

        Assert.AreEqual(value.Hours().From(originalPointInTime), originalPointInTime.AddHours(value));
        Assert.AreEqual(value.Minutes().From(originalPointInTime), originalPointInTime.AddMinutes(value));
        Assert.AreEqual(value.Seconds().From(originalPointInTime), originalPointInTime.AddSeconds(value));
        Assert.AreEqual(value.Milliseconds().From(originalPointInTime), originalPointInTime.AddMilliseconds(value));
        Assert.AreEqual(value.Ticks().From(originalPointInTime), originalPointInTime.AddTicks(value));
    }
开发者ID:jin29neci,项目名称:FluentDateTime,代码行数:15,代码来源:DateTimeOffsetTests.cs

示例11: AdmiralRankingPoints

        internal AdmiralRankingPoints(Admiral rpAdmiral)
        {
            r_Owner = rpAdmiral;
            r_AdmiralID = rpAdmiral.ID;

            ApiService.SubscribeOnce("api_port/port", delegate
            {
                ReloadInitialRankingPoints();

                PreviousUpdateDifference = new AdmiralRankingPointsDifference(this, AdmiralRankingPointsDifferenceType.PreviousUpdate);
                DayDifference = new AdmiralRankingPointsDifference(this, AdmiralRankingPointsDifferenceType.Day);
                MonthDifference = new AdmiralRankingPointsDifference(this, AdmiralRankingPointsDifferenceType.Month);

                Update();

                var rRankUpdateTime = new DateTimeOffset(DateTimeOffset.Now.Date, TimeSpan.FromHours(6.0));
                var rDayTimeSpan = TimeSpan.FromDays(1.0);
                Observable.Timer(rRankUpdateTime.AddDays(1.0), rDayTimeSpan).Subscribe(delegate
                {
                    PreviousUpdateDifference.Reload();
                    DayDifference.Reload();
                    OnPropertyChanged(nameof(PreviousUpdateDifference));
                    OnPropertyChanged(nameof(DayDifference));
                });

                rRankUpdateTime += TimeSpan.FromHours(12.0);
                if (DateTimeOffset.Now > rRankUpdateTime)
                    rRankUpdateTime += rDayTimeSpan;
                Observable.Timer(rRankUpdateTime, rDayTimeSpan).Subscribe(delegate
                {
                    PreviousUpdateDifference.Reload();
                    OnPropertyChanged(nameof(PreviousUpdateDifference));
                });

                var rNow = DateTimeOffset.Now;
                var rFinalizationTime = new DateTimeOffset(rNow.Year, rNow.Month, 1, 0, 0, 0, TimeSpan.FromHours(9.0)).AddMonths(1).AddHours(-2.0);
                if (rNow >= rFinalizationTime)
                    FinalizeThisMonth();
                else
                    Observable.Return(Unit.Default).Delay(rFinalizationTime).Subscribe(_ => FinalizeThisMonth());

                Observable.Return(Unit.Default).Delay(rFinalizationTime.AddHours(2.0)).Subscribe(_ => r_IsFinalized = false);

                RecordService.Instance.Update += RecordService_Update;
            });
        }
开发者ID:amatukaze,项目名称:IntelligentNavalGun,代码行数:46,代码来源:AdmiralRankingPoints.cs

示例12: Snippets

        void Snippets()
        {
            #region Dividing
            var now = new DateTimeOffset(2013, 8, 13, 13, 0, 0, TimeSpan.FromHours(12));
            var availableTime = new TimeRange(now, now.AddHours(1));
            var freeSlots = availableTime.Divide(TimeSpan.FromMinutes(15));
            foreach (var freeSlot in freeSlots)
            {
                Console.WriteLine(freeSlot);
            }

            // Produces 4 15 minute time ranges.
            //
            // 13 Aug 2013 1:00:00 p.m. +12:00 for 00:15:00
            // 13 Aug 2013 1:15:00 p.m. +12:00 for 00:15:00
            // 13 Aug 2013 1:30:00 p.m. +12:00 for 00:15:00
            // 13 Aug 2013 1:45:00 p.m. +12:00 for 00:15:00
            #endregion
        }
开发者ID:richardschneider,项目名称:sepia,代码行数:19,代码来源:TimeRangeExample.cs

示例13: TranslatedAdd

 private static DateTimeOffset TranslatedAdd(DateTimeOffset date, IntervalUnit unit, int amountToAdd)
 {
     switch (unit)
     {
         case IntervalUnit.Day:
             return date.AddDays(amountToAdd);
         case IntervalUnit.Hour:
             return date.AddHours(amountToAdd);
         case IntervalUnit.Minute:
             return date.AddMinutes(amountToAdd);
         case IntervalUnit.Month:
             return date.AddMonths(amountToAdd);
         case IntervalUnit.Second:
             return date.AddSeconds(amountToAdd);
         case IntervalUnit.Millisecond:
             return date.AddMilliseconds(amountToAdd);
         case IntervalUnit.Week:
             return date.AddDays(amountToAdd*7);
         case IntervalUnit.Year:
             return date.AddYears(amountToAdd);
         default:
             throw new ArgumentException("Unknown IntervalUnit");
     }
 }
开发者ID:quartznet,项目名称:quartznet,代码行数:24,代码来源:DateBuilder.cs

示例14: Round

		public static DateTimeOffset Round(this DateTimeOffset dateTime, RoundTo rt)
		{
			DateTimeOffset rounded;

		    switch (rt)
			{
				case RoundTo.Second:
					{
                        rounded = new DateTimeOffset(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second,dateTime.Offset);
						if (dateTime.Millisecond >= 500)
						{
							rounded = rounded.AddSeconds(1);
						}
						break;
					}
				case RoundTo.Minute:
					{
                        rounded = new DateTimeOffset(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, 0,dateTime.Offset);
						if (dateTime.Second >= 30)
						{
							rounded = rounded.AddMinutes(1);
						}
						break;
					}
				case RoundTo.Hour:
					{
                        rounded = new DateTimeOffset(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0,dateTime.Offset);
						if (dateTime.Minute >= 30)
						{
							rounded = rounded.AddHours(1);
						}
						break;
					}
				case RoundTo.Day:
					{
                        rounded = new DateTimeOffset(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0,dateTime.Offset);
						if (dateTime.Hour >= 12)
						{
							rounded = rounded.AddDays(1);
						}
						break;
					}
				default:
					{
                        throw new ArgumentOutOfRangeException("rt");
					}
			}

			return rounded;
		}
开发者ID:jin29neci,项目名称:FluentDateTime,代码行数:50,代码来源:DateTimeExtensions.cs

示例15: TestHourlyIntervalGetFireTimeAfter

        public void TestHourlyIntervalGetFireTimeAfter()
        {
            DateTimeOffset startCalendar = new DateTimeOffset(2005, 6, 1, 9, 30, 17, TimeSpan.Zero);

            CalendarIntervalTriggerImpl yearlyTrigger = new CalendarIntervalTriggerImpl();
            yearlyTrigger.StartTimeUtc = startCalendar;
            yearlyTrigger.RepeatIntervalUnit = IntervalUnit.Hour;
            yearlyTrigger.RepeatInterval = 100; // every 100 hours

            DateTimeOffset targetCalendar = new DateTimeOffset(2005, 6, 1, 9, 30, 17, TimeSpan.Zero);

            targetCalendar = targetCalendar.AddHours(400); // jump 400 hours (4 intervals)

            IList<DateTimeOffset> fireTimes = TriggerUtils.ComputeFireTimes(yearlyTrigger, null, 6);
            DateTimeOffset fifthTime = fireTimes[4]; // get the fifth fire time

            Assert.AreEqual(targetCalendar, fifthTime, "Hour increment result not as expected.");
        }
开发者ID:haf,项目名称:quartznet,代码行数:18,代码来源:CalendarIntervalTriggerTest.cs


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