Struct ZonedDateTime
Inherited Members
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
[Serializable]
public struct ZonedDateTime : IEquatable<ZonedDateTime>, IFormattable, IXmlSerializable, ISerializable
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Constructors
ZonedDateTime(Instant, DateTimeZone)
Declaration
public ZonedDateTime(Instant instant, DateTimeZone zone)
Parameters
Type | Name | Description |
---|---|---|
Instant | instant | The instant. |
DateTimeZone | zone | The time zone. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Exceptions
Type | Condition |
---|---|
ArgumentNullException | zone is null. |
ZonedDateTime(Instant, DateTimeZone, CalendarSystem)
Declaration
public ZonedDateTime(Instant instant, DateTimeZone zone, CalendarSystem calendar)
Parameters
Type | Name | Description |
---|---|---|
Instant | instant | The instant. |
DateTimeZone | zone | The time zone. |
CalendarSystem | calendar | The calendar system. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Exceptions
Type | Condition |
---|---|
ArgumentNullException | zone or calendar is null |
ZonedDateTime(LocalDateTime, DateTimeZone, Offset)
Declaration
public ZonedDateTime(LocalDateTime localDateTime, DateTimeZone zone, Offset offset)
Parameters
Type | Name | Description |
---|---|---|
LocalDateTime | localDateTime | The local date and time. |
DateTimeZone | zone | The time zone. |
Offset | offset | The offset between UTC and local time at the desired instant. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Exceptions
Type | Condition |
---|---|
ArgumentException | offset is not a valid offset at the given
local date and time. |
ArgumentNullException | zone is null. |
Properties
Calendar
Declaration
public CalendarSystem Calendar { get; }
Property Value
Type | Description |
---|---|
CalendarSystem | The calendar system associated with this zoned date and time. (The value returned is never null.) |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
ClockHourOfHalfDay
Declaration
public int ClockHourOfHalfDay { get; }
Property Value
Type | Description |
---|---|
Int32 | The hour of the half-day of this zoned date and time, in the range 1 to 12 inclusive. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Date
Declaration
public LocalDate Date { get; }
Property Value
Type | Description |
---|---|
LocalDate | The local date represented by this zoned date and time. |
Remarks
Day
Declaration
public int Day { get; }
Property Value
Type | Description |
---|---|
Int32 | The day of this zoned date and time within the month. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
DayOfWeek
Declaration
public IsoDayOfWeek DayOfWeek { get; }
Property Value
Type | Description |
---|---|
IsoDayOfWeek | The week day of this zoned date and time expressed as an IsoDayOfWeek value. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
DayOfYear
Declaration
public int DayOfYear { get; }
Property Value
Type | Description |
---|---|
Int32 | The day of this zoned date and time within the year. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Era
Declaration
public Era Era { get; }
Property Value
Type | Description |
---|---|
Era | The era for this zoned date and time. (The value returned is never null.) |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Hour
Declaration
public int Hour { get; }
Property Value
Type | Description |
---|---|
Int32 | The hour of day of this zoned date and time, in the range 0 to 23 inclusive. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
LocalDateTime
Declaration
public LocalDateTime LocalDateTime { get; }
Property Value
Type | Description |
---|---|
LocalDateTime | The local date and time represented by this zoned date and time. |
Remarks
Millisecond
Declaration
public int Millisecond { get; }
Property Value
Type | Description |
---|---|
Int32 | The millisecond of this zoned date and time within the second, in the range 0 to 999 inclusive. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Minute
Declaration
public int Minute { get; }
Property Value
Type | Description |
---|---|
Int32 | The minute of this zoned date and time, in the range 0 to 59 inclusive. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Month
Declaration
public int Month { get; }
Property Value
Type | Description |
---|---|
Int32 | The month of this zoned date and time within the year. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
NanosecondOfDay
Declaration
public long NanosecondOfDay { get; }
Property Value
Type | Description |
---|---|
Int64 | The nanosecond of this zoned date and time within the day, in the range 0 to 86,399,999,999,999 inclusive. |
Remarks
Sample snippet
using NodaTime;
using System;
// This is a 25-hour day at the end of daylight saving time
var dt = new LocalDate(2017, 10, 29);
var time = new LocalTime(23, 59, 59);
var dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
var startOfDay = dublin.AtStartOfDay(dt);
ZonedDateTime nearEndOfDay = dublin.AtStrictly(dt + time);
Console.WriteLine(nearEndOfDay.NanosecondOfDay);
Duration duration = nearEndOfDay - startOfDay;
Console.WriteLine(duration);
Output:
86399000000000
1:00:59:59
NanosecondOfSecond
Declaration
public int NanosecondOfSecond { get; }
Property Value
Type | Description |
---|---|
Int32 | The nanosecond of this zoned date and time within the second, in the range 0 to 999,999,999 inclusive. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Offset
Declaration
public Offset Offset { get; }
Property Value
Type | Description |
---|---|
Offset | The offset of the local representation of this value from UTC. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Second
Declaration
public int Second { get; }
Property Value
Type | Description |
---|---|
Int32 | The second of this zoned date and time within the minute, in the range 0 to 59 inclusive. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
TickOfDay
Declaration
public long TickOfDay { get; }
Property Value
Type | Description |
---|---|
Int64 | The tick of this zoned date and time within the day, in the range 0 to 863,999,999,999 inclusive. |
Remarks
Sample snippet
using NodaTime;
using System;
// This is a 25-hour day at the end of daylight saving time
var dt = new LocalDate(2017, 10, 29);
var time = new LocalTime(23, 59, 59);
var dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
var startOfDay = dublin.AtStartOfDay(dt);
ZonedDateTime nearEndOfDay = dublin.AtStrictly(dt + time);
Console.WriteLine(nearEndOfDay.TickOfDay);
Duration duration = nearEndOfDay - startOfDay;
Console.WriteLine(duration);
Output:
863990000000
1:00:59:59
TickOfSecond
Declaration
public int TickOfSecond { get; }
Property Value
Type | Description |
---|---|
Int32 | The tick of this zoned date and time within the second, in the range 0 to 9,999,999 inclusive. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
TimeOfDay
Declaration
public LocalTime TimeOfDay { get; }
Property Value
Type | Description |
---|---|
LocalTime | The time portion of this zoned date and time. |
Remarks
Year
Declaration
public int Year { get; }
Property Value
Type | Description |
---|---|
Int32 | The year of this zoned date and time. |
Remarks
YearOfEra
Declaration
public int YearOfEra { get; }
Property Value
Type | Description |
---|---|
Int32 | The year of this zoned date and time within its era. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Zone
Declaration
public DateTimeZone Zone { get; }
Property Value
Type | Description |
---|---|
DateTimeZone | The time zone associated with this value. (The value returned is never null.) |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Methods
Add(ZonedDateTime, Duration)
Declaration
public static ZonedDateTime Add(ZonedDateTime zonedDateTime, Duration duration)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | zonedDateTime | The value to add the duration to. |
Duration | duration | The duration to add |
Returns
Type | Description |
---|---|
ZonedDateTime | A new value with the time advanced by the given duration, in the same calendar system and time zone. |
Remarks
Sample snippet
using NodaTime;
using System;
// Europe/Dublin transitions from UTC+1 to UTC+0 at 2am (local) on 2017-10-29
var dt = new LocalDateTime(2017, 10, 29, 1, 45, 0);
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
ZonedDateTime beforeTransition = new ZonedDateTime(dt, dublin, Offset.FromHours(1));
var result = ZonedDateTime.Add(beforeTransition, Duration.FromHours(1));
Console.WriteLine(result.Date);
// Adding an hour of elapsed time takes us across the DST transition, so we have
// the same local time (shown on a clock) but a different offset.
Console.WriteLine(result);
// The + operator and Plus instance method are equivalent to the Add static method.
var result2 = beforeTransition + Duration.FromHours(1);
var result3 = beforeTransition.Plus(Duration.FromHours(1));
Console.WriteLine(result2);
Console.WriteLine(result3);
Output:
Sunday, 29 October 2017
2017-10-29T01:45:00 Europe/Dublin (+00)
2017-10-29T01:45:00 Europe/Dublin (+00)
2017-10-29T01:45:00 Europe/Dublin (+00)
Deconstruct(out LocalDateTime, out DateTimeZone, out Offset)
Declaration
public void Deconstruct(out LocalDateTime localDateTime, out DateTimeZone dateTimeZone, out Offset offset)
Parameters
Type | Name | Description |
---|---|---|
LocalDateTime | localDateTime | The LocalDateTime component. |
DateTimeZone | dateTimeZone | The DateTimeZone component. |
Offset | offset | The Offset component. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Equals(ZonedDateTime)
Declaration
public bool Equals(ZonedDateTime other)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | other | An object to compare with this object. |
Returns
Type | Description |
---|---|
Boolean |
true if the current object is equal to the other parameter; otherwise, false.
|
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Equals(Object)
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | Another object to compare to. |
Returns
Type | Description |
---|---|
Boolean |
true if obj and this instance are the same type and represent the same value; otherwise, false.
|
Overrides
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
FromDateTimeOffset(DateTimeOffset)
Declaration
public static ZonedDateTime FromDateTimeOffset(DateTimeOffset dateTimeOffset)
Parameters
Type | Name | Description |
---|---|---|
DateTimeOffset | dateTimeOffset | Date and time value with an offset. |
Returns
Type | Description |
---|---|
ZonedDateTime | A ZonedDateTime value representing the same instant in time as the given DateTimeOffset. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
GetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 | A 32-bit signed integer that is the hash code for this instance. |
Overrides
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
GetZoneInterval()
Declaration
public ZoneInterval GetZoneInterval()
Returns
Type | Description |
---|---|
ZoneInterval | The ZoneInterval containing this value. (The value returned is never null.) |
Remarks
ZoneInterval
containing that instant.
IsDaylightSavingTime()
Declaration
public bool IsDaylightSavingTime()
Returns
Type | Description |
---|---|
Boolean | true if the zone interval containing this value has a non-zero savings
component; false otherwise. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Sample snippet
using NodaTime;
using System;
// Europe/Dublin transitions from UTC+1 to UTC+0 at 2am (local) on 2017-10-29
var dt = new LocalDateTime(2017, 10, 29, 1, 45, 0);
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
ZonedDateTime beforeTransition = new ZonedDateTime(dt, dublin, Offset.FromHours(1));
Console.WriteLine(beforeTransition.IsDaylightSavingTime());
// Same local time, different offset - so a different instant, after the transition.
ZonedDateTime afterTransition = new ZonedDateTime(dt, dublin, Offset.FromHours(0));
Console.WriteLine(afterTransition.IsDaylightSavingTime());
Output:
False
True
See Also
Minus(Duration)
Declaration
public ZonedDateTime Minus(Duration duration)
Parameters
Type | Name | Description |
---|---|---|
Duration | duration | The duration to subtract |
Returns
Type | Description |
---|---|
ZonedDateTime | A new ZonedDateTime representing the result of the subtraction. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Sample snippet
using NodaTime;
using System;
// Europe/Dublin transitions from UTC+1 to UTC+0 at 2am (local) on 2017-10-29
var dt = new LocalDateTime(2017, 10, 29, 1, 45, 0);
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
ZonedDateTime afterTransition = new ZonedDateTime(dt, dublin, Offset.FromHours(0));
var result = ZonedDateTime.Subtract(afterTransition, Duration.FromHours(1));
Console.WriteLine(result.Date);
// Adding an hour of elapsed time takes us across the DST transition, so we have
// the same local time (shown on a clock) but a different offset.
Console.WriteLine(result);
// The + operator and Plus instance method are equivalent to the Add static method.
var result2 = afterTransition - Duration.FromHours(1);
var result3 = afterTransition.Minus(Duration.FromHours(1));
Console.WriteLine(result2);
Console.WriteLine(result3);
Output:
Sunday, 29 October 2017
2017-10-29T01:45:00 Europe/Dublin (+01)
2017-10-29T01:45:00 Europe/Dublin (+01)
2017-10-29T01:45:00 Europe/Dublin (+01)
Minus(ZonedDateTime)
Declaration
public Duration Minus(ZonedDateTime other)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | other | The zoned date and time to subtract from this one. |
Returns
Type | Description |
---|---|
Duration | The elapsed duration from other to this value. |
Remarks
Sample snippet
using NodaTime;
using System;
var zone = DateTimeZone.ForOffset(Offset.FromHours(-5));
ZonedDateTime subject = new ZonedDateTime(Instant.FromUtc(2017, 7, 17, 7, 17), zone);
ZonedDateTime other = new ZonedDateTime(Instant.FromUtc(2017, 7, 17, 9, 17), zone);
var difference = other.Minus(subject);
Console.WriteLine(difference);
Output:
0:02:00:00
Plus(Duration)
Declaration
public ZonedDateTime Plus(Duration duration)
Parameters
Type | Name | Description |
---|---|---|
Duration | duration | The duration to add |
Returns
Type | Description |
---|---|
ZonedDateTime | A new ZonedDateTime representing the result of the addition. |
Remarks
Sample snippet
using NodaTime;
using System;
// Europe/Dublin transitions from UTC+1 to UTC+0 at 2am (local) on 2017-10-29
var dt = new LocalDateTime(2017, 10, 29, 1, 45, 0);
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
ZonedDateTime beforeTransition = new ZonedDateTime(dt, dublin, Offset.FromHours(1));
var result = ZonedDateTime.Add(beforeTransition, Duration.FromHours(1));
Console.WriteLine(result.Date);
// Adding an hour of elapsed time takes us across the DST transition, so we have
// the same local time (shown on a clock) but a different offset.
Console.WriteLine(result);
// The + operator and Plus instance method are equivalent to the Add static method.
var result2 = beforeTransition + Duration.FromHours(1);
var result3 = beforeTransition.Plus(Duration.FromHours(1));
Console.WriteLine(result2);
Console.WriteLine(result3);
Output:
Sunday, 29 October 2017
2017-10-29T01:45:00 Europe/Dublin (+00)
2017-10-29T01:45:00 Europe/Dublin (+00)
2017-10-29T01:45:00 Europe/Dublin (+00)
PlusHours(Int32)
Declaration
public ZonedDateTime PlusHours(int hours)
Parameters
Type | Name | Description |
---|---|---|
Int32 | hours | The number of hours to add |
Returns
Type | Description |
---|---|
ZonedDateTime | A new ZonedDateTime representing the result of the addition. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Sample snippet
using NodaTime;
using NodaTime.Text;
using System;
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
var start = Instant.FromUtc(2017, 7, 20, 5, 30);
// Dublin is at UTC+1 in July 2017, so this is 6:30am.
ZonedDateTime zoned = new ZonedDateTime(start, dublin);
var pattern = ZonedDateTimePattern.ExtendedFormatOnlyIso;
Console.WriteLine(pattern.Format(zoned.PlusHours(1)));
Output:
2017-07-20T07:30:00 Europe/Dublin (+01)
PlusMilliseconds(Int64)
Declaration
public ZonedDateTime PlusMilliseconds(long milliseconds)
Parameters
Type | Name | Description |
---|---|---|
Int64 | milliseconds | The number of milliseconds to add |
Returns
Type | Description |
---|---|
ZonedDateTime | A new ZonedDateTime representing the result of the addition. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Sample snippet
using NodaTime;
using NodaTime.Text;
using System;
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
var start = Instant.FromUtc(2017, 7, 20, 5, 30);
// Dublin is at UTC+1 in July 2017, so this is 6:30am.
ZonedDateTime zoned = new ZonedDateTime(start, dublin);
var pattern = ZonedDateTimePattern.ExtendedFormatOnlyIso;
Console.WriteLine(pattern.Format(zoned.PlusMilliseconds(1)));
Output:
2017-07-20T06:30:00.001 Europe/Dublin (+01)
PlusMinutes(Int32)
Declaration
public ZonedDateTime PlusMinutes(int minutes)
Parameters
Type | Name | Description |
---|---|---|
Int32 | minutes | The number of minutes to add |
Returns
Type | Description |
---|---|
ZonedDateTime | A new ZonedDateTime representing the result of the addition. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Sample snippet
using NodaTime;
using NodaTime.Text;
using System;
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
var start = Instant.FromUtc(2017, 7, 20, 5, 30);
// Dublin is at UTC+1 in July 2017, so this is 6:30am.
ZonedDateTime zoned = new ZonedDateTime(start, dublin);
var pattern = ZonedDateTimePattern.ExtendedFormatOnlyIso;
Console.WriteLine(pattern.Format(zoned.PlusMinutes(1)));
Output:
2017-07-20T06:31:00 Europe/Dublin (+01)
PlusNanoseconds(Int64)
Declaration
public ZonedDateTime PlusNanoseconds(long nanoseconds)
Parameters
Type | Name | Description |
---|---|---|
Int64 | nanoseconds | The number of nanoseconds to add |
Returns
Type | Description |
---|---|
ZonedDateTime | A new ZonedDateTime representing the result of the addition. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Sample snippet
using NodaTime;
using NodaTime.Text;
using System;
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
var start = Instant.FromUtc(2017, 7, 20, 5, 30);
// Dublin is at UTC+1 in July 2017, so this is 6:30am.
ZonedDateTime zoned = new ZonedDateTime(start, dublin);
var pattern = ZonedDateTimePattern.ExtendedFormatOnlyIso;
Console.WriteLine(pattern.Format(zoned.PlusNanoseconds(1)));
Output:
2017-07-20T06:30:00.000000001 Europe/Dublin (+01)
PlusSeconds(Int64)
Declaration
public ZonedDateTime PlusSeconds(long seconds)
Parameters
Type | Name | Description |
---|---|---|
Int64 | seconds | The number of seconds to add |
Returns
Type | Description |
---|---|
ZonedDateTime | A new ZonedDateTime representing the result of the addition. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Sample snippet
using NodaTime;
using NodaTime.Text;
using System;
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
var start = Instant.FromUtc(2017, 7, 20, 5, 30);
// Dublin is at UTC+1 in July 2017, so this is 6:30am.
ZonedDateTime zoned = new ZonedDateTime(start, dublin);
var pattern = ZonedDateTimePattern.ExtendedFormatOnlyIso;
Console.WriteLine(pattern.Format(zoned.PlusSeconds(1)));
Output:
2017-07-20T06:30:01 Europe/Dublin (+01)
PlusTicks(Int64)
Declaration
public ZonedDateTime PlusTicks(long ticks)
Parameters
Type | Name | Description |
---|---|---|
Int64 | ticks | The number of ticks to add |
Returns
Type | Description |
---|---|
ZonedDateTime | A new ZonedDateTime representing the result of the addition. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Sample snippet
using NodaTime;
using NodaTime.Text;
using System;
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
var start = Instant.FromUtc(2017, 7, 20, 5, 30);
// Dublin is at UTC+1 in July 2017, so this is 6:30am.
ZonedDateTime zoned = new ZonedDateTime(start, dublin);
var pattern = ZonedDateTimePattern.ExtendedFormatOnlyIso;
Console.WriteLine(pattern.Format(zoned.PlusTicks(1)));
Output:
2017-07-20T06:30:00.0000001 Europe/Dublin (+01)
Subtract(ZonedDateTime, Duration)
Declaration
public static ZonedDateTime Subtract(ZonedDateTime zonedDateTime, Duration duration)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | zonedDateTime | The value to subtract the duration from. |
Duration | duration | The duration to subtract. |
Returns
Type | Description |
---|---|
ZonedDateTime | A new value with the time "rewound" by the given duration, in the same calendar system and time zone. |
Remarks
Sample snippet
using NodaTime;
using System;
// Europe/Dublin transitions from UTC+1 to UTC+0 at 2am (local) on 2017-10-29
var dt = new LocalDateTime(2017, 10, 29, 1, 45, 0);
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
ZonedDateTime afterTransition = new ZonedDateTime(dt, dublin, Offset.FromHours(0));
var result = ZonedDateTime.Subtract(afterTransition, Duration.FromHours(1));
Console.WriteLine(result.Date);
// Adding an hour of elapsed time takes us across the DST transition, so we have
// the same local time (shown on a clock) but a different offset.
Console.WriteLine(result);
// The + operator and Plus instance method are equivalent to the Add static method.
var result2 = afterTransition - Duration.FromHours(1);
var result3 = afterTransition.Minus(Duration.FromHours(1));
Console.WriteLine(result2);
Console.WriteLine(result3);
Output:
Sunday, 29 October 2017
2017-10-29T01:45:00 Europe/Dublin (+01)
2017-10-29T01:45:00 Europe/Dublin (+01)
2017-10-29T01:45:00 Europe/Dublin (+01)
Subtract(ZonedDateTime, ZonedDateTime)
Declaration
public static Duration Subtract(ZonedDateTime end, ZonedDateTime start)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | end | The zoned date and time value to subtract from; if this is later than start
then the result will be positive. |
ZonedDateTime | start | The zoned date and time to subtract from end . |
Returns
Type | Description |
---|---|
Duration | The elapsed duration from start to end . |
Remarks
Sample snippet
using NodaTime;
using System;
var zone = DateTimeZone.ForOffset(Offset.FromHours(-5));
ZonedDateTime subject = new ZonedDateTime(Instant.FromUtc(2017, 7, 17, 7, 17), zone);
ZonedDateTime other = new ZonedDateTime(Instant.FromUtc(2017, 7, 17, 9, 17), zone);
var difference = ZonedDateTime.Subtract(other, subject);
Console.WriteLine(difference);
Output:
0:02:00:00
ToDateTimeOffset()
Declaration
public DateTimeOffset ToDateTimeOffset()
Returns
Type | Description |
---|---|
DateTimeOffset | A DateTimeOffset with the same local date/time and offset as this. The DateTime part of
the result always has a "kind" of Unspecified. |
Remarks
An offset does not convey as much information as a time zone; a DateTimeOffset represents an instant in time along with an associated local time, but it doesn't allow you to find out what the local time would be for another instant.
If the date and time is not on a tick boundary (the unit of granularity of DateTime) the value will be truncated towards the start of time.
If the offset has a non-zero second component, this is truncated as DateTimeOffset
has an offset
granularity of minutes.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | The date/time is outside the range of DateTimeOffset ,
or the offset is outside the range of +/-14 hours (the range supported by DateTimeOffset ). |
ToDateTimeUnspecified()
Declaration
public DateTime ToDateTimeUnspecified()
Returns
Type | Description |
---|---|
DateTime | A DateTime representation of this value with an "unspecified" kind, with the same local date and time as this value. |
Remarks
Unspecified is slightly odd - it can be treated as UTC if you use ToLocalTime() or as system local time if you use ToUniversalTime(), but it's the only kind which allows you to construct a DateTimeOffset with an arbitrary offset.
If the date and time is not on a tick boundary (the unit of granularity of DateTime) the value will be truncated towards the start of time.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | The date/time is outside the range of DateTime . |
ToDateTimeUtc()
Declaration
public DateTime ToDateTimeUtc()
Returns
Type | Description |
---|---|
DateTime | A DateTime representation of this value with a "universal" kind, with the same instant of time as this value. |
Remarks
If the date and time is not on a tick boundary (the unit of granularity of DateTime) the value will be truncated towards the start of time.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | The final date/time is outside the range of DateTime . |
ToInstant()
Declaration
public Instant ToInstant()
Returns
Type | Description |
---|---|
Instant | The instant corresponding to this value. |
Remarks
ZonedDateTime
remembers
the actual offset from UTC to local time, so it always knows the exact instant represented.
ToOffsetDateTime()
Declaration
public OffsetDateTime ToOffsetDateTime()
Returns
Type | Description |
---|---|
OffsetDateTime | An OffsetDateTime with the same local date/time and offset as this value. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
ToString()
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String | The value of the current instance in the default format pattern ("G"), using the current thread's culture to obtain a format provider. |
Overrides
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
ToString(String, IFormatProvider)
Declaration
public string ToString(string patternText, IFormatProvider formatProvider)
Parameters
Type | Name | Description |
---|---|---|
String | patternText | The String specifying the pattern to use, or null to use the default format pattern ("G"). |
IFormatProvider | formatProvider | The IFormatProvider to use when formatting the value, or null to use the current thread's culture to obtain a format provider. |
Returns
Type | Description |
---|---|
String | A String containing the value of the current instance in the specified format. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
WithCalendar(CalendarSystem)
Declaration
public ZonedDateTime WithCalendar(CalendarSystem calendar)
Parameters
Type | Name | Description |
---|---|---|
CalendarSystem | calendar | The calendar system to convert this zoned date and time to. |
Returns
Type | Description |
---|---|
ZonedDateTime | The converted ZonedDateTime. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Exceptions
Type | Condition |
---|---|
ArgumentNullException | calendar is null. |
WithZone(DateTimeZone)
Declaration
public ZonedDateTime WithZone(DateTimeZone targetZone)
Parameters
Type | Name | Description |
---|---|---|
DateTimeZone | targetZone | The target time zone to convert to. |
Returns
Type | Description |
---|---|
ZonedDateTime | A new value in the target time zone. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Exceptions
Type | Condition |
---|---|
ArgumentNullException | targetZone is null. |
Operators
Addition(ZonedDateTime, Duration)
Declaration
public static ZonedDateTime operator +(ZonedDateTime zonedDateTime, Duration duration)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | zonedDateTime | The ZonedDateTime to add the duration to. |
Duration | duration | The duration to add. |
Returns
Type | Description |
---|---|
ZonedDateTime | A new value with the time advanced by the given duration, in the same calendar system and time zone. |
Remarks
zonedDateTime
.
Sample snippet
using NodaTime;
using System;
// Europe/Dublin transitions from UTC+1 to UTC+0 at 2am (local) on 2017-10-29
var dt = new LocalDateTime(2017, 10, 29, 1, 45, 0);
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
ZonedDateTime beforeTransition = new ZonedDateTime(dt, dublin, Offset.FromHours(1));
var result = ZonedDateTime.Add(beforeTransition, Duration.FromHours(1));
Console.WriteLine(result.Date);
// Adding an hour of elapsed time takes us across the DST transition, so we have
// the same local time (shown on a clock) but a different offset.
Console.WriteLine(result);
// The + operator and Plus instance method are equivalent to the Add static method.
var result2 = beforeTransition + Duration.FromHours(1);
var result3 = beforeTransition.Plus(Duration.FromHours(1));
Console.WriteLine(result2);
Console.WriteLine(result3);
Output:
Sunday, 29 October 2017
2017-10-29T01:45:00 Europe/Dublin (+00)
2017-10-29T01:45:00 Europe/Dublin (+00)
2017-10-29T01:45:00 Europe/Dublin (+00)
Equality(ZonedDateTime, ZonedDateTime)
Declaration
public static bool operator ==(ZonedDateTime left, ZonedDateTime right)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | left | The first value to compare |
ZonedDateTime | right | The second value to compare |
Returns
Type | Description |
---|---|
Boolean | True if the two operands are equal according to Equals(ZonedDateTime); false otherwise |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Inequality(ZonedDateTime, ZonedDateTime)
Declaration
public static bool operator !=(ZonedDateTime left, ZonedDateTime right)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | left | The first value to compare |
ZonedDateTime | right | The second value to compare |
Returns
Type | Description |
---|---|
Boolean | False if the two operands are equal according to Equals(ZonedDateTime); true otherwise |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Subtraction(ZonedDateTime, Duration)
Declaration
public static ZonedDateTime operator -(ZonedDateTime zonedDateTime, Duration duration)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | zonedDateTime | The value to subtract the duration from. |
Duration | duration | The duration to subtract. |
Returns
Type | Description |
---|---|
ZonedDateTime | A new value with the time "rewound" by the given duration, in the same calendar system and time zone. |
Remarks
zonedDateTime
.
Sample snippet
using NodaTime;
using System;
// Europe/Dublin transitions from UTC+1 to UTC+0 at 2am (local) on 2017-10-29
var dt = new LocalDateTime(2017, 10, 29, 1, 45, 0);
DateTimeZone dublin = DateTimeZoneProviders.Tzdb["Europe/Dublin"];
ZonedDateTime afterTransition = new ZonedDateTime(dt, dublin, Offset.FromHours(0));
var result = ZonedDateTime.Subtract(afterTransition, Duration.FromHours(1));
Console.WriteLine(result.Date);
// Adding an hour of elapsed time takes us across the DST transition, so we have
// the same local time (shown on a clock) but a different offset.
Console.WriteLine(result);
// The + operator and Plus instance method are equivalent to the Add static method.
var result2 = afterTransition - Duration.FromHours(1);
var result3 = afterTransition.Minus(Duration.FromHours(1));
Console.WriteLine(result2);
Console.WriteLine(result3);
Output:
Sunday, 29 October 2017
2017-10-29T01:45:00 Europe/Dublin (+01)
2017-10-29T01:45:00 Europe/Dublin (+01)
2017-10-29T01:45:00 Europe/Dublin (+01)
Subtraction(ZonedDateTime, ZonedDateTime)
Declaration
public static Duration operator -(ZonedDateTime end, ZonedDateTime start)
Parameters
Type | Name | Description |
---|---|---|
ZonedDateTime | end | The zoned date and time value to subtract from; if this is later than start
then the result will be positive. |
ZonedDateTime | start | The zoned date and time to subtract from end . |
Returns
Type | Description |
---|---|
Duration | The elapsed duration from start to end . |
Remarks
end.ToInstant() - start.ToInstant()
; in particular:
- The two values can use different calendar systems
- The two values can be in different time zones
- The two values can have different UTC offsets
Explicit Interface Implementations
ISerializable.GetObjectData(SerializationInfo, StreamingContext)
Declaration
[SecurityCritical]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
Type | Name | Description |
---|---|---|
SerializationInfo | info | The SerializationInfo to populate with data. |
StreamingContext | context | The destination for this serialization. |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
IXmlSerializable.GetSchema()
Declaration
XmlSchema IXmlSerializable.GetSchema()
Returns
Type | Description |
---|---|
XmlSchema |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
IXmlSerializable.ReadXml(XmlReader)
Declaration
void IXmlSerializable.ReadXml(XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
XmlReader | reader |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Exceptions
Type | Condition |
---|---|
ArgumentNullException | reader is null. |
IXmlSerializable.WriteXml(XmlWriter)
Declaration
void IXmlSerializable.WriteXml(XmlWriter writer)
Parameters
Type | Name | Description |
---|---|---|
XmlWriter | writer |
Remarks
Although ZonedDateTime includes both local and global concepts, it only supports duration-based - and not calendar-based - arithmetic. This avoids ambiguities and skipped date/time values becoming a problem within a series of calculations; instead, these can be considered just once, at the point of conversion to a ZonedDateTime.
ZonedDateTime
does not implement ordered comparison operators, as there is no obvious natural ordering that works in all cases.
Equality is supported however, requiring equality of zone, calendar and date/time. If you want to sort ZonedDateTime
values, you should explicitly choose one of the orderings provided via the static properties in the
ZonedDateTime.Comparer nested class (or implement your own comparison).
Exceptions
Type | Condition |
---|---|
ArgumentNullException | writer is null. |