Struct OffsetDateTime
A local date and time in a particular calendar system, combined with an offset from UTC. This is
broadly similar to System.DateTimeOffset in the BCL.
Since 1.0.x
Availability net6.0, netstandard2.0
Implements
IFormattable
IXmlSerializable
Inherited Members
Object.Equals(Object, Object)
Object.GetType()
Object.ReferenceEquals(Object, Object)
Assembly: NodaTime.dll
Syntax
[TypeConverter(typeof(OffsetDateTimeTypeConverter))]
public readonly struct OffsetDateTime : IEquatable<OffsetDateTime>, IFormattable, IXmlSerializable
Constructors
OffsetDateTime(LocalDateTime, Offset)
Constructs a new offset date/time with the given local date and time, and the given offset from UTC.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public OffsetDateTime(LocalDateTime localDateTime, Offset offset)
Parameters
Type |
Name |
Description |
LocalDateTime |
localDateTime |
Local date and time to represent |
Offset |
offset |
Offset from UTC |
Sample snippet
using NodaTime;
using System;
LocalDateTime localDateTime = new LocalDateTime(1985, 10, 26, 1, 18);
Offset offset = Offset.FromHours(-5);
OffsetDateTime offsetDateTime = new OffsetDateTime(localDateTime, offset);
Console.WriteLine(offsetDateTime.LocalDateTime);
Console.WriteLine(offsetDateTime.Offset);
Output:
10/26/1985 01:18:00
-05
Properties
Calendar
Gets the calendar system associated with this offset date and time.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly CalendarSystem Calendar { get; }
Property Value
Type |
Description |
CalendarSystem |
The calendar system associated with this offset date and time. |
ClockHourOfHalfDay
Gets the hour of the half-day of this offset date and time, in the range 1 to 12 inclusive.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int ClockHourOfHalfDay { get; }
Property Value
Type |
Description |
Int32 |
The hour of the half-day of this offset date and time, in the range 1 to 12 inclusive. |
Date
Gets the local date represented by this offset date and time.
Since 1.1.x
Availability net6.0, netstandard2.0
Declaration
public readonly LocalDate Date { get; }
Property Value
Type |
Description |
LocalDate |
The local date represented by this offset date and time. |
Day
Gets the day of this offset date and time within the month.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int Day { get; }
Property Value
Type |
Description |
Int32 |
The day of this offset date and time within the month. |
DayOfWeek
Gets the week day of this offset date and time expressed as an
IsoDayOfWeek value.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly IsoDayOfWeek DayOfWeek { get; }
Property Value
Type |
Description |
IsoDayOfWeek |
The week day of this offset date and time expressed as an IsoDayOfWeek . |
DayOfYear
Gets the day of this offset date and time within the year.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int DayOfYear { get; }
Property Value
Type |
Description |
Int32 |
The day of this offset date and time within the year. |
Era
Gets the era of this offset date and time.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly Era Era { get; }
Property Value
Type |
Description |
Era |
The era of this offset date and time. |
Hour
Gets the hour of day of this offset date and time, in the range 0 to 23 inclusive.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int Hour { get; }
Property Value
Type |
Description |
Int32 |
The hour of day of this offset date and time, in the range 0 to 23 inclusive. |
LocalDateTime
Returns the local date and time represented within this offset date and time.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly LocalDateTime LocalDateTime { get; }
Property Value
Type |
Description |
LocalDateTime |
The local date and time represented within this offset date and time. |
Millisecond
Gets the millisecond of this offset date and time within the second, in the range 0 to 999 inclusive.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int Millisecond { get; }
Property Value
Type |
Description |
Int32 |
The millisecond of this offset date and time within the second, in the range 0 to 999 inclusive. |
Minute
Gets the minute of this offset date and time, in the range 0 to 59 inclusive.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int Minute { get; }
Property Value
Type |
Description |
Int32 |
The minute of this offset date and time, in the range 0 to 59 inclusive. |
Month
Gets the month of this offset date and time within the year.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int Month { get; }
Property Value
Type |
Description |
Int32 |
The month of this offset date and time within the year. |
NanosecondOfDay
Gets the nanosecond of this offset date and time within the day, in the range 0 to 86,399,999,999,999 inclusive.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly long NanosecondOfDay { get; }
Property Value
Type |
Description |
Int64 |
The nanosecond of this offset date and time within the day, in the range 0 to 86,399,999,999,999 inclusive. |
NanosecondOfSecond
Gets the nanosecond of this offset date and time within the second, in the range 0 to 999,999,999 inclusive.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int NanosecondOfSecond { get; }
Property Value
Type |
Description |
Int32 |
The nanosecond of this offset date and time within the second, in the range 0 to 999,999,999 inclusive. |
Offset
Gets the offset from UTC.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly Offset Offset { get; }
Property Value
Type |
Description |
Offset |
The offset from UTC. |
Second
Gets the second of this offset date and time within the minute, in the range 0 to 59 inclusive.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int Second { get; }
Property Value
Type |
Description |
Int32 |
The second of this offset date and time within the minute, in the range 0 to 59 inclusive. |
TickOfDay
Gets the tick of this offset date and time within the day, in the range 0 to 863,999,999,999 inclusive.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly long TickOfDay { get; }
Property Value
Type |
Description |
Int64 |
The tick of this offset date and time within the day, in the range 0 to 863,999,999,999 inclusive. |
TickOfSecond
Gets the tick of this offset date and time within the second, in the range 0 to 9,999,999 inclusive.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int TickOfSecond { get; }
Property Value
Type |
Description |
Int32 |
The tick of this offset date and time within the second, in the range 0 to 9,999,999 inclusive. |
TimeOfDay
Gets the time portion of this offset date and time.
Since 1.1.x
Availability net6.0, netstandard2.0
Declaration
public readonly LocalTime TimeOfDay { get; }
Property Value
Type |
Description |
LocalTime |
The time portion of this offset date and time. |
Year
Gets the year of this offset date and time.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int Year { get; }
Property Value
Type |
Description |
Int32 |
The year of this offset date and time. |
YearOfEra
Gets the year of this offset date and time within the era.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int YearOfEra { get; }
Property Value
Type |
Description |
Int32 |
The year of this offset date and time within the era. |
Methods
Add(OffsetDateTime, Duration)
Adds a duration to an offset date and time.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public static OffsetDateTime Add(OffsetDateTime offsetDateTime, Duration duration)
Parameters
Type |
Name |
Description |
OffsetDateTime |
offsetDateTime |
The value to add the duration to. |
Duration |
duration |
The duration to add |
Returns
Type |
Description |
OffsetDateTime |
A new value with the time advanced by the given duration, in the same calendar system and with the same offset. |
AddSchema(XmlSchemaSet)
Adds the XML schema type describing the structure of the
OffsetDateTime XML serialization to the given
xmlSchemaSet
.
Since 3.0.x
Availability net6.0, netstandard2.0
Declaration
public static XmlQualifiedName AddSchema(XmlSchemaSet xmlSchemaSet)
Parameters
Type |
Name |
Description |
XmlSchemaSet |
xmlSchemaSet |
The XML schema set provided by System.Xml.Serialization.XmlSchemaExporter. |
Returns
Type |
Description |
XmlQualifiedName |
The qualified name of the schema type that was added to the xmlSchemaSet . |
Deconstruct(out LocalDate, out LocalTime, out Offset)
Since 2.3.x
Availability net6.0, netstandard2.0
Declaration
public readonly void Deconstruct(out LocalDate localDate, out LocalTime localTime, out Offset offset)
Parameters
Deconstruct(out LocalDateTime, out Offset)
Since 2.3.x
Availability net6.0, netstandard2.0
Declaration
public readonly void Deconstruct(out LocalDateTime localDateTime, out Offset offset)
Parameters
Equals(OffsetDateTime)
Compares two
OffsetDateTime values for equality.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly bool Equals(OffsetDateTime other)
Parameters
Type |
Name |
Description |
OffsetDateTime |
other |
The value to compare this offset date/time with. |
Returns
Type |
Description |
Boolean |
True if the given value is another offset date/time equal to this one; false otherwise. |
Equals(Object)
Compares two
OffsetDateTime values for equality.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public override readonly bool Equals(object obj)
Parameters
Type |
Name |
Description |
Object |
obj |
The object to compare this date with. |
Returns
Type |
Description |
Boolean |
True if the given value is another offset date/time equal to this one; false otherwise. |
Overrides
System.ValueType.Equals(System.Object)
FromDateTimeOffset(DateTimeOffset)
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public static OffsetDateTime FromDateTimeOffset(DateTimeOffset dateTimeOffset)
Parameters
Type |
Name |
Description |
DateTimeOffset |
dateTimeOffset |
DateTimeOffset to convert |
Returns
GetHashCode()
Returns a hash code for this offset date and time.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public override readonly int GetHashCode()
Returns
Type |
Description |
Int32 |
A hash code for this offset date and time. |
Overrides
System.ValueType.GetHashCode()
InFixedZone()
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly ZonedDateTime InFixedZone()
Returns
Type |
Description |
ZonedDateTime |
A zoned date/time with the same local time and a fixed time zone using the offset from this value. |
InZone(DateTimeZone)
Returns this value in ths specified time zone. This method does not expect
the offset in the zone to be the same as for the current value; it simply converts
this value into an
Instant and finds the
ZonedDateTime
for that instant in the specified zone.
Since 2.3.x
Availability net6.0, netstandard2.0
Declaration
public readonly ZonedDateTime InZone(DateTimeZone zone)
Parameters
Type |
Name |
Description |
DateTimeZone |
zone |
The time zone of the new value. |
Returns
Type |
Description |
ZonedDateTime |
The instant represented by this value, in the specified time zone. |
Minus(Duration)
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime Minus(Duration duration)
Parameters
Type |
Name |
Description |
Duration |
duration |
The duration to subtract |
Returns
Minus(OffsetDateTime)
Returns the result of subtracting another offset 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, netstandard2.0
Declaration
public readonly Duration Minus(OffsetDateTime other)
Parameters
Type |
Name |
Description |
OffsetDateTime |
other |
The offset date and time to subtract from this one. |
Returns
Type |
Description |
Duration |
The elapsed duration from other to this value. |
Plus(Duration)
Returns the result of adding a duration to this offset date and time.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime Plus(Duration duration)
Parameters
Type |
Name |
Description |
Duration |
duration |
The duration to add |
Returns
PlusHours(Int32)
Returns the result of adding a increment of hours to this offset date and time
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime PlusHours(int hours)
Parameters
Type |
Name |
Description |
Int32 |
hours |
The number of hours to add |
Returns
PlusMilliseconds(Int64)
Returns the result of adding an increment of milliseconds to this offset date and time
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime PlusMilliseconds(long milliseconds)
Parameters
Type |
Name |
Description |
Int64 |
milliseconds |
The number of milliseconds to add |
Returns
PlusMinutes(Int32)
Returns the result of adding an increment of minutes to this offset date and time
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime PlusMinutes(int minutes)
Parameters
Type |
Name |
Description |
Int32 |
minutes |
The number of minutes to add |
Returns
PlusNanoseconds(Int64)
Returns the result of adding an increment of nanoseconds to this offset date and time
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime PlusNanoseconds(long nanoseconds)
Parameters
Type |
Name |
Description |
Int64 |
nanoseconds |
The number of nanoseconds to add |
Returns
PlusSeconds(Int64)
Returns the result of adding an increment of seconds to this offset date and time
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime PlusSeconds(long seconds)
Parameters
Type |
Name |
Description |
Int64 |
seconds |
The number of seconds to add |
Returns
PlusTicks(Int64)
Returns the result of adding an increment of ticks to this offset date and time
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime PlusTicks(long ticks)
Parameters
Type |
Name |
Description |
Int64 |
ticks |
The number of ticks to add |
Returns
Subtract(OffsetDateTime, Duration)
Subtracts a duration from an offset date and time.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public static OffsetDateTime Subtract(OffsetDateTime offsetDateTime, Duration duration)
Parameters
Type |
Name |
Description |
OffsetDateTime |
offsetDateTime |
The value to subtract the duration from. |
Duration |
duration |
The duration to subtract. |
Returns
Type |
Description |
OffsetDateTime |
A new value with the time "rewound" by the given duration, in the same calendar system and with the same offset. |
Subtract(OffsetDateTime, OffsetDateTime)
Subtracts one offset date and time from another, returning an elapsed duration.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public static Duration Subtract(OffsetDateTime end, OffsetDateTime start)
Parameters
Type |
Name |
Description |
OffsetDateTime |
end |
The offset date and time value to subtract from; if this is later than start
then the result will be positive. |
OffsetDateTime |
start |
The offset date and time to subtract from end . |
Returns
Type |
Description |
Duration |
The elapsed duration from start to end . |
ToDateTimeOffset()
Returns the BCL System.DateTimeOffset corresponding to this offset date and time.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly DateTimeOffset ToDateTimeOffset()
Returns
Type |
Description |
DateTimeOffset |
A DateTimeOffset with the same local date/time and offset as this. The System.DateTime part of
the result always has a "kind" of Unspecified. |
Exceptions
Type |
Condition |
System.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 ). |
ToInstant()
Converts this offset date and time to an instant in time by subtracting the offset from the local date and time.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly Instant ToInstant()
Returns
Type |
Description |
Instant |
The instant represented by this offset date and time |
ToOffsetDate()
Constructs a new
OffsetDate from the date and offset of this value,
but omitting the time-of-day.
Since 2.3.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDate ToOffsetDate()
Returns
Type |
Description |
OffsetDate |
A value representing the date and offset aspects of this value. |
ToOffsetTime()
Constructs a new
OffsetTime from the time and offset of this value,
but omitting the date.
Since 2.3.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetTime ToOffsetTime()
Returns
Type |
Description |
OffsetTime |
A value representing the time and offset aspects of this value. |
ToString()
Returns a System.String that represents this instance.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public override readonly 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
System.ValueType.ToString()
Formats the value of the current instance using the specified pattern.
Since 1.2.x
Availability net6.0, netstandard2.0
Declaration
public readonly string ToString(string patternText, IFormatProvider formatProvider)
Parameters
Type |
Name |
Description |
String |
patternText |
The System.String specifying the pattern to use,
or null to use the default format pattern ("G").
|
IFormatProvider |
formatProvider |
The System.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 System.String containing the value of the current instance in the specified format.
|
With(Func<LocalDate, LocalDate>)
Returns this offset date/time, with the given date adjuster applied to it, maintaining the existing time of day and offset.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime With(Func<LocalDate, LocalDate> adjuster)
Parameters
Returns
Sample snippet
using NodaTime;
using System;
LocalDateTime localDateTime = new LocalDateTime(1985, 10, 26, 1, 18);
Offset offset = Offset.FromHours(-5);
OffsetDateTime original = new OffsetDateTime(localDateTime, offset);
var dateAdjuster = DateAdjusters.AddPeriod(Period.FromYears(30));
OffsetDateTime updated = original.With(dateAdjuster);
Console.WriteLine(updated.LocalDateTime);
Console.WriteLine(updated.Offset);
Output:
10/26/2015 01:18:00
-05
With(Func<LocalTime, LocalTime>)
Returns this date/time, with the given time adjuster applied to it, maintaining the existing date and offset.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime With(Func<LocalTime, LocalTime> adjuster)
Parameters
Returns
Sample snippet
using NodaTime;
using System;
LocalDateTime localDateTime = new LocalDateTime(1985, 10, 26, 1, 18);
Offset offset = Offset.FromHours(-5);
OffsetDateTime original = new OffsetDateTime(localDateTime, offset);
OffsetDateTime updated = original.With(TimeAdjusters.TruncateToHour);
Console.WriteLine(updated.LocalDateTime);
Console.WriteLine(updated.Offset);
Output:
10/26/1985 01:00:00
-05
WithCalendar(CalendarSystem)
Creates a new OffsetDateTime representing the same physical date, time and offset, but in a different calendar.
The returned OffsetDateTime 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 1.2.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime WithCalendar(CalendarSystem calendar)
Parameters
Type |
Name |
Description |
CalendarSystem |
calendar |
The calendar system to convert this offset date and time to. |
Returns
Sample snippet
using NodaTime;
using System;
LocalDateTime localDateTime = new LocalDateTime(1985, 10, 26, 1, 18, CalendarSystem.Iso);
Offset offset = Offset.FromHours(-5);
OffsetDateTime original = new OffsetDateTime(localDateTime, offset);
OffsetDateTime updated = original.WithCalendar(CalendarSystem.Julian);
Console.WriteLine(updated.LocalDateTime.ToString("r", null));
Console.WriteLine(updated.Offset);
Output:
1985-10-13T01:18:00.000000000 (Julian)
-05
WithOffset(Offset)
Creates a new OffsetDateTime representing the instant in time in the same calendar,
but with a different offset. The local date and time is adjusted accordingly.
Since 1.3.x
Availability net6.0, netstandard2.0
Declaration
public readonly OffsetDateTime WithOffset(Offset offset)
Parameters
Type |
Name |
Description |
Offset |
offset |
The new offset to use. |
Returns
Sample snippet
using NodaTime;
using System;
LocalDateTime localDateTime = new LocalDateTime(1985, 10, 26, 1, 18);
Offset offset = Offset.FromHours(-3);
OffsetDateTime original = new OffsetDateTime(localDateTime, offset);
OffsetDateTime updated = original.WithOffset(Offset.FromHours(-2));
Console.WriteLine(updated.LocalDateTime);
Console.WriteLine(updated.Offset);
Output:
10/26/1985 02:18:00
-02
Operators
Addition(OffsetDateTime, Duration)
Returns a new
OffsetDateTime with the time advanced by the given duration.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public static OffsetDateTime operator +(OffsetDateTime offsetDateTime, Duration duration)
Parameters
Returns
Type |
Description |
OffsetDateTime |
A new value with the time advanced by the given duration, in the same calendar system and with the same offset. |
Equality(OffsetDateTime, OffsetDateTime)
Implements the operator == (equality).
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public static bool operator ==(OffsetDateTime left, OffsetDateTime right)
Parameters
Returns
Type |
Description |
Boolean |
true if values are equal to each other, otherwise false . |
Inequality(OffsetDateTime, OffsetDateTime)
Implements the operator != (inequality).
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public static bool operator !=(OffsetDateTime left, OffsetDateTime right)
Parameters
Returns
Type |
Description |
Boolean |
true if values are not equal to each other, otherwise false . |
Subtraction(OffsetDateTime, Duration)
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public static OffsetDateTime operator -(OffsetDateTime offsetDateTime, Duration duration)
Parameters
Type |
Name |
Description |
OffsetDateTime |
offsetDateTime |
The value to subtract the duration from. |
Duration |
duration |
The duration to subtract. |
Returns
Type |
Description |
OffsetDateTime |
A new value with the time "rewound" by the given duration, in the same calendar system and with the same offset. |
Subtraction(OffsetDateTime, OffsetDateTime)
Subtracts one
OffsetDateTime from another, resulting in the elapsed time between
the two values.
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public static Duration operator -(OffsetDateTime end, OffsetDateTime start)
Parameters
Type |
Name |
Description |
OffsetDateTime |
end |
The offset date and time value to subtract from; if this is later than start
then the result will be positive. |
OffsetDateTime |
start |
The offset date and time to subtract from end . |
Returns
Type |
Description |
Duration |
The elapsed duration from start to end . |
Explicit Interface Implementations
IXmlSerializable.ReadXml(XmlReader)
Since 1.2.x
Availability net6.0, netstandard2.0
Declaration
readonly void IXmlSerializable.ReadXml(XmlReader reader)
Parameters
Type |
Name |
Description |
XmlReader |
reader |
|
IXmlSerializable.WriteXml(XmlWriter)
Since 1.2.x
Availability net6.0, netstandard2.0
Declaration
readonly void IXmlSerializable.WriteXml(XmlWriter writer)
Parameters
Type |
Name |
Description |
XmlWriter |
writer |
|
Implements
System.IEquatable<T>
System.IFormattable
System.Xml.Serialization.IXmlSerializable