Struct ZonedDateTime
A
LocalDateTime in a specific time zone and with a particular offset to distinguish
between otherwise-ambiguous instants. A
ZonedDateTime is global, in that it maps to a single
Instant.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Assembly: NodaTime.dll
Syntax
[TypeConverter(typeof(ZonedDateTimeTypeConverter))]
public readonly struct ZonedDateTime : IEquatable<ZonedDateTime>, IFormattable, IXmlSerializable, IAdditionOperators<ZonedDateTime, Duration, ZonedDateTime>, ISubtractionOperators<ZonedDateTime, ZonedDateTime, Duration>, ISubtractionOperators<ZonedDateTime, Duration, ZonedDateTime>, IEqualityOperators<ZonedDateTime, ZonedDateTime, bool>
Constructors
ZonedDateTime(Instant, DateTimeZone)
Initializes a new instance of the
ZonedDateTime struct in the specified time zone
and the ISO calendar.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime(Instant instant, DateTimeZone zone)
Parameters
ZonedDateTime(Instant, DateTimeZone, CalendarSystem)
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime(Instant instant, DateTimeZone zone, CalendarSystem calendar)
Parameters
ZonedDateTime(LocalDateTime, DateTimeZone, Offset)
Initializes a new instance of the
ZonedDateTime struct in the specified time zone
from a given local time and offset. The offset is validated to be correct as part of initialization.
In most cases a local time can only map to a single instant anyway, but the offset is included here for cases
where the local time is ambiguous, usually due to daylight saving transitions.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
Exceptions
Type |
Condition |
ArgumentException |
offset is not a valid offset at the given
local date and time. |
Properties
Calendar
Gets the calendar system associated with this zoned date and time.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public CalendarSystem Calendar { get; }
Property Value
Type |
Description |
CalendarSystem |
The calendar system associated with this zoned date and time. |
ClockHourOfHalfDay
Gets the hour of the half-day of this zoned date and time, in the range 1 to 12 inclusive.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int ClockHourOfHalfDay { get; }
Property Value
Type |
Description |
int |
The hour of the half-day of this zoned date and time, in the range 1 to 12 inclusive. |
Date
Gets the local date represented by this zoned date and time.
Since 1.1.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDate Date { get; }
Property Value
Type |
Description |
LocalDate |
The local date represented by this zoned date and time. |
Day
Gets the day of this zoned date and time within the month.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
Property Value
Type |
Description |
int |
The day of this zoned date and time within the month. |
DayOfWeek
Gets the week day of this zoned date and time expressed as an
IsoDayOfWeek value.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public IsoDayOfWeek DayOfWeek { get; }
Property Value
Type |
Description |
IsoDayOfWeek |
The week day of this zoned date and time expressed as an IsoDayOfWeek value. |
DayOfYear
Gets the day of this zoned date and time within the year.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int DayOfYear { get; }
Property Value
Type |
Description |
int |
The day of this zoned date and time within the year. |
Era
Gets the era for this zoned date and time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
Property Value
Type |
Description |
Era |
The era for this zoned date and time. |
Hour
Gets the hour of day of this zoned date and time, in the range 0 to 23 inclusive.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
Property Value
Type |
Description |
int |
The hour of day of this zoned date and time, in the range 0 to 23 inclusive. |
LocalDateTime
Gets the local date and time represented by this zoned date and time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDateTime LocalDateTime { get; }
Property Value
Type |
Description |
LocalDateTime |
The local date and time represented by this zoned date and time. |
Millisecond
Gets the millisecond of this zoned date and time within the second, in the range 0 to 999 inclusive.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Millisecond { get; }
Property Value
Type |
Description |
int |
The millisecond of this zoned date and time within the second, in the range 0 to 999 inclusive. |
Minute
Gets the minute of this zoned date and time, in the range 0 to 59 inclusive.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Minute { get; }
Property Value
Type |
Description |
int |
The minute of this zoned date and time, in the range 0 to 59 inclusive. |
Month
Gets the month of this zoned date and time within the year.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Month { get; }
Property Value
Type |
Description |
int |
The month of this zoned date and time within the year. |
NanosecondOfDay
Gets the nanosecond of this zoned date and time within the day, in the range 0 to 86,399,999,999,999 inclusive.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long NanosecondOfDay { get; }
Property Value
Type |
Description |
long |
The nanosecond of this zoned date and time within the day, in the range 0 to 86,399,999,999,999 inclusive. |
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
Gets the nanosecond of this zoned date and time within the second, in the range 0 to 999,999,999 inclusive.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int NanosecondOfSecond { get; }
Property Value
Type |
Description |
int |
The nanosecond of this zoned date and time within the second, in the range 0 to 999,999,999 inclusive. |
Offset
Gets the offset of the local representation of this value from UTC.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Offset Offset { get; }
Property Value
Type |
Description |
Offset |
The offset of the local representation of this value from UTC. |
Second
Gets the second of this zoned date and time within the minute, in the range 0 to 59 inclusive.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Second { get; }
Property Value
Type |
Description |
int |
The second of this zoned date and time within the minute, in the range 0 to 59 inclusive. |
TickOfDay
Gets the tick of this zoned date and time within the day, in the range 0 to 863,999,999,999 inclusive.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long TickOfDay { get; }
Property Value
Type |
Description |
long |
The tick of this zoned date and time within the day, in the range 0 to 863,999,999,999 inclusive. |
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
Gets the tick of this zoned date and time within the second, in the range 0 to 9,999,999 inclusive.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int TickOfSecond { get; }
Property Value
Type |
Description |
int |
The tick of this zoned date and time within the second, in the range 0 to 9,999,999 inclusive. |
TimeOfDay
Gets the time portion of this zoned date and time.
Since 1.1.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalTime TimeOfDay { get; }
Property Value
Type |
Description |
LocalTime |
The time portion of this zoned date and time. |
Year
Gets the year of this zoned date and time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
Property Value
Type |
Description |
int |
The year of this zoned date and time. |
YearOfEra
Gets the year of this zoned date and time within its era.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int YearOfEra { get; }
Property Value
Type |
Description |
int |
The year of this zoned date and time within its era. |
Zone
Gets the time zone associated with this value.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public DateTimeZone Zone { get; }
Property Value
Type |
Description |
DateTimeZone |
The time zone associated with this value. |
Methods
Add(ZonedDateTime, Duration)
Adds a duration to a zoned date and time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
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)
AddSchema(XmlSchemaSet)
Adds the XML schema type describing the structure of the
ZonedDateTime XML serialization to the given
xmlSchemaSet
.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static XmlQualifiedName AddSchema(XmlSchemaSet xmlSchemaSet)
Parameters
Returns
Type |
Description |
XmlQualifiedName |
The qualified name of the schema type that was added to the xmlSchemaSet . |
Deconstruct(out LocalDateTime, out DateTimeZone, out Offset)
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public void Deconstruct(out LocalDateTime localDateTime, out DateTimeZone dateTimeZone, out Offset offset)
Parameters
Equals(ZonedDateTime)
Indicates whether the current object is equal to another object of the same type.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool Equals(ZonedDateTime other)
Parameters
Type |
Name |
Description |
ZonedDateTime |
other |
An object to compare with this object. |
Returns
Type |
Description |
bool |
true if the current object is equal to the other parameter; otherwise, false. |
Equals(object?)
Indicates whether this instance and a specified object are equal.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override bool Equals(object? obj)
Parameters
Type |
Name |
Description |
object |
obj |
Another object to compare to. |
Returns
Type |
Description |
bool |
true if obj and this instance are the same type and represent the same value; otherwise, false. |
Overrides
FromDateTimeOffset(DateTimeOffset)
Returns a new
ZonedDateTime representing the same instant in time as the given
DateTimeOffset.
The time zone used will be a fixed time zone, which uses the same offset throughout time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static ZonedDateTime FromDateTimeOffset(DateTimeOffset dateTimeOffset)
Parameters
Type |
Name |
Description |
DateTimeOffset |
dateTimeOffset |
Date and time value with an offset. |
Returns
GetHashCode()
Computes the hash code for this instance.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override int GetHashCode()
Returns
Type |
Description |
int |
A 32-bit signed integer that is the hash code for this instance. |
Overrides
GetZoneInterval()
Returns the
ZoneInterval containing this value, in the time zone this
value refers to.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZoneInterval GetZoneInterval()
Returns
Type |
Description |
ZoneInterval |
The ZoneInterval containing this value. |
IsDaylightSavingTime()
Indicates whether or not this
ZonedDateTime is in daylight saving time
for its time zone. This is determined by checking the
Savings property
of the zone interval containing this value.
Since 1.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool IsDaylightSavingTime()
Returns
Type |
Description |
bool |
true if the zone interval containing this value has a non-zero savings
component; false otherwise. |
Sample snippet
using NodaTime;
using System;
// Europe/Dublin transitions from UTC+1 to UTC+0 at 2am (local) on 2017-10-29
// However, Europe/Dublin is also odd in terms of having its standard time as *summer* time,
// and its winter time as "daylight saving time". The saving offset in winter is -1 hour,
// as opposed to the more common "+1 hour in summer".
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)
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime Minus(Duration duration)
Parameters
Type |
Name |
Description |
Duration |
duration |
The duration to subtract |
Returns
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)
Returns the result of subtracting another zoned date and time from this one, resulting in the elapsed duration
between the two instants represented in the values.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
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)
Returns the result of adding a duration to this zoned date and time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime Plus(Duration duration)
Parameters
Type |
Name |
Description |
Duration |
duration |
The duration to add |
Returns
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(int)
Returns the result of adding a increment of hours to this zoned date and time
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime PlusHours(int hours)
Parameters
Type |
Name |
Description |
int |
hours |
The number of hours to add |
Returns
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(long)
Returns the result of adding an increment of milliseconds to this zoned date and time
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime PlusMilliseconds(long milliseconds)
Parameters
Type |
Name |
Description |
long |
milliseconds |
The number of milliseconds to add |
Returns
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(int)
Returns the result of adding an increment of minutes to this zoned date and time
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime PlusMinutes(int minutes)
Parameters
Type |
Name |
Description |
int |
minutes |
The number of minutes to add |
Returns
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(long)
Returns the result of adding an increment of nanoseconds to this zoned date and time
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime PlusNanoseconds(long nanoseconds)
Parameters
Type |
Name |
Description |
long |
nanoseconds |
The number of nanoseconds to add |
Returns
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(long)
Returns the result of adding an increment of seconds to this zoned date and time
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime PlusSeconds(long seconds)
Parameters
Type |
Name |
Description |
long |
seconds |
The number of seconds to add |
Returns
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(long)
Returns the result of adding an increment of ticks to this zoned date and time
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime PlusTicks(long ticks)
Parameters
Type |
Name |
Description |
long |
ticks |
The number of ticks to add |
Returns
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)
Subtracts a duration from a zoned date and time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
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)
Subtracts one zoned date and time from another, returning an elapsed duration.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
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 . |
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()
Constructs a
DateTimeOffset value with the same local time and offset from
UTC as this value.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
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()
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
Exceptions
ToDateTimeUtc()
Constructs a
DateTime from this
ZonedDateTime which has a
Kind of
Utc and represents the same instant of time as
this value rather than the same local time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
Exceptions
ToInstant()
Converts this value to the instant it represents on the time line.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Instant ToInstant()
Returns
Type |
Description |
Instant |
The instant corresponding to this value. |
ToOffsetDateTime()
Constructs an
OffsetDateTime with the same local date and time, and the same offset
as this zoned date and time, effectively just "removing" the time zone itself.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public OffsetDateTime ToOffsetDateTime()
Returns
Type |
Description |
OffsetDateTime |
An OffsetDateTime with the same local date/time and offset as this value. |
ToString()
Returns a
string that represents this instance.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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
Formats the value of the current instance using the specified pattern.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
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. |
WithCalendar(CalendarSystem)
Creates a new ZonedDateTime representing the same physical date, time and offset, but in a different calendar.
The returned ZonedDateTime is likely to have different date field values to this one.
For example, January 1st 1970 in the Gregorian calendar was December 19th 1969 in the Julian calendar.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public ZonedDateTime WithCalendar(CalendarSystem calendar)
Parameters
Type |
Name |
Description |
CalendarSystem |
calendar |
The calendar system to convert this zoned date and time to. |
Returns
WithZone(DateTimeZone)
Creates a new
ZonedDateTime representing the same instant in time, in the
same calendar but a different time zone.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
Operators
operator +(ZonedDateTime, Duration)
Returns a new
ZonedDateTime with the time advanced by the given duration. Note that
due to daylight saving time changes this may not advance the local time by the same amount.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static ZonedDateTime operator +(ZonedDateTime zonedDateTime, Duration duration)
Parameters
Returns
Type |
Description |
ZonedDateTime |
A new value with the time advanced by the given duration, in the same calendar system and time zone. |
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)
operator ==(ZonedDateTime, ZonedDateTime)
Implements the operator ==.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator ==(ZonedDateTime left, ZonedDateTime right)
Parameters
Returns
operator !=(ZonedDateTime, ZonedDateTime)
Implements the operator !=.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator !=(ZonedDateTime left, ZonedDateTime right)
Parameters
Returns
operator -(ZonedDateTime, Duration)
Returns a new
ZonedDateTime with the duration subtracted. Note that
due to daylight saving time changes this may not change the local time by the same amount.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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. |
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)
operator -(ZonedDateTime, ZonedDateTime)
Subtracts one
ZonedDateTime from another, resulting in the elapsed time between
the two values.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
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 . |
Explicit Interface Implementations
IXmlSerializable.GetSchema()
This method is reserved and should not be used. When implementing the
IXmlSerializable
interface, you should return
null (
Nothing
in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the
XmlSchemaProviderAttribute to the class.
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
XmlSchema IXmlSerializable.GetSchema()
Returns
IXmlSerializable.ReadXml(XmlReader)
Generates an object from its XML representation.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
void IXmlSerializable.ReadXml(XmlReader reader)
Parameters
Type |
Name |
Description |
XmlReader |
reader |
The XmlReader stream from which the object is deserialized. |
IXmlSerializable.WriteXml(XmlWriter)
Converts an object into its XML representation.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
void IXmlSerializable.WriteXml(XmlWriter writer)
Parameters
Type |
Name |
Description |
XmlWriter |
writer |
The XmlWriter stream to which the object is serialized. |
Implements