Struct OffsetDate
A combination of a LocalDate and an Offset, to represent
a date at a specific offset from UTC but without any time-of-day information.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Implements
Inherited Members
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
[TypeConverter(typeof(OffsetDateTypeConverter))]
public readonly struct OffsetDate : IEquatable<OffsetDate>, IXmlSerializable, IFormattable, IEqualityOperators<OffsetDate, OffsetDate, bool>
Remarks
Equality is defined in a component-wise fashion: two values are the same if they represent equal dates (including being in the same calendar) and equal offsets from UTC. Ordering between offset dates is not defined.
The default value of this type is 0001-01-01 (January 1st, 1 C.E.) in the ISO calendar with a UTC offset of zero.
Constructors
OffsetDate(LocalDate, Offset)
Constructs an instance of the specified date and offset.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public OffsetDate(LocalDate date, Offset offset)
Parameters
Type | Name | Description |
---|---|---|
LocalDate | date | The date part of the value. |
Offset | offset | The offset part of the value. |
Sample snippet
using NodaTime;
using System;
OffsetDate offsetDate = new OffsetDate(
new LocalDate(2019, 5, 3),
Offset.FromHours(3));
Console.WriteLine(offsetDate.Offset);
Console.WriteLine(offsetDate.Date);
Output:
+03
Friday, 03 May 2019
Properties
Calendar
Gets the calendar system associated with this offset date.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public CalendarSystem Calendar { get; }
Property Value
Type | Description |
---|---|
CalendarSystem | The calendar system associated with this offset date. |
Date
Gets the local date represented by this value.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDate Date { get; }
Property Value
Type | Description |
---|---|
LocalDate | The local date represented by this value. |
Day
Gets the day of this offset date within the month.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Day { get; }
Property Value
Type | Description |
---|---|
int | The day of this offset date within the month. |
DayOfWeek
Gets the week day of this offset date expressed as an IsoDayOfWeek value.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public IsoDayOfWeek DayOfWeek { get; }
Property Value
Type | Description |
---|---|
IsoDayOfWeek | The week day of this offset date expressed as an IsoDayOfWeek . |
DayOfYear
Gets the day of this offset date within the year.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int DayOfYear { get; }
Property Value
Type | Description |
---|---|
int | The day of this offset date within the year. |
Era
Gets the era of this offset date.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Era Era { get; }
Property Value
Type | Description |
---|---|
Era | The era of this offset date. |
Month
Gets the month of this offset date within the year.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Month { get; }
Property Value
Type | Description |
---|---|
int | The month of this offset date within the year. |
Offset
Gets the offset from UTC of this value.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Offset Offset { get; }
Property Value
Type | Description |
---|---|
Offset | The offset from UTC of this value. |
Year
Gets the year of this offset date.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Year { get; }
Property Value
Type | Description |
---|---|
int | The year of this offset date. |
Remarks
This returns the "absolute year", so, for the ISO calendar,
a value of 0 means 1 BC, for example.
YearOfEra
Gets the year of this offset date within the era.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int YearOfEra { get; }
Property Value
Type | Description |
---|---|
int | The year of this offset date within the era. |
Methods
AddSchema(XmlSchemaSet)
Adds the XML schema type describing the structure of the OffsetDate 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
Type | Name | Description |
---|---|---|
XmlSchemaSet | xmlSchemaSet | The XML schema set provided by XmlSchemaExporter. |
Returns
Type | Description |
---|---|
XmlQualifiedName | The qualified name of the schema type that was added to the xmlSchemaSet . |
At(LocalTime)
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public OffsetDateTime At(LocalTime time)
Parameters
Type | Name | Description |
---|---|---|
LocalTime | time | The time to combine with this date. |
Returns
Type | Description |
---|---|
OffsetDateTime | The OffsetDateTime representation of the given time on this date. |
Deconstruct(out LocalDate, out Offset)
Deconstruct this value into its components.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public void Deconstruct(out LocalDate localDate, out Offset offset)
Parameters
Type | Name | Description |
---|---|---|
LocalDate | localDate | The LocalDate component. |
Offset | offset | The Offset component. |
Equals(OffsetDate)
Compares two OffsetDate values for equality.
See the type documentation for a description of equality semantics.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool Equals(OffsetDate other)
Parameters
Type | Name | Description |
---|---|---|
OffsetDate | other | The value to compare this offset date with. |
Returns
Type | Description |
---|---|
bool | True if the given value is another offset date equal to this one; false otherwise. |
Equals(object?)
Compares two OffsetDate values for equality.
See the type documentation for a description of equality semantics.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override bool Equals(object? obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare this offset date with. |
Returns
Type | Description |
---|---|
bool | True if the given value is another offset date equal to this one; false otherwise. |
Overrides
GetHashCode()
Returns a hash code for this offset date.
See the type documentation for a description of equality semantics.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | A hash code for this offset date. |
Overrides
ToString()
Returns a string that represents this instance.
Since 2.3.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
ToString(string?, IFormatProvider?)
Formats the value of the current instance using the specified pattern.
Since 2.3.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. |
With(Func<LocalDate, LocalDate>)
Returns this offset date, with the given date adjuster applied to it, maintaining the existing offset.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public OffsetDate With(Func<LocalDate, LocalDate> adjuster)
Parameters
Type | Name | Description |
---|---|---|
Func<LocalDate, LocalDate> | adjuster | The adjuster to apply. |
Returns
Type | Description |
---|---|
OffsetDate | The adjusted offset date. |
Remarks
If the adjuster attempts to construct an
invalid date (such as by trying to set a day-of-month of 30 in February), any exception thrown by
that construction attempt will be propagated through this method.
Sample snippet
using NodaTime;
using System;
var original = new OffsetDate(
new LocalDate(2019, 5, 3),
Offset.FromHours(3));
Func<LocalDate, LocalDate> tomorrowAdjuster = x => x + Period.FromDays(1);
OffsetDate updated = original.With(tomorrowAdjuster);
Console.WriteLine(updated.Offset);
Console.WriteLine(updated.Date);
Output:
+03
Saturday, 04 May 2019
WithCalendar(CalendarSystem)
Creates a new OffsetDate representing the same physical date and offset, but in a different calendar.
The returned value 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.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public OffsetDate WithCalendar(CalendarSystem calendar)
Parameters
Type | Name | Description |
---|---|---|
CalendarSystem | calendar | The calendar system to convert this offset date to. |
Returns
Type | Description |
---|---|
OffsetDate | The converted OffsetDate . |
Sample snippet
using NodaTime;
using System;
var original = new OffsetDate(
new LocalDate(2019, 5, 3, CalendarSystem.Iso),
Offset.FromHours(3));
OffsetDate updated = original.WithCalendar(CalendarSystem.Gregorian);
Console.WriteLine(updated.Offset);
Console.WriteLine(updated.Calendar);
Output:
+03
Gregorian
WithOffset(Offset)
Creates a new OffsetDate for the same date, but with the specified UTC offset.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public OffsetDate WithOffset(Offset offset)
Parameters
Type | Name | Description |
---|---|---|
Offset | offset | The new UTC offset. |
Returns
Type | Description |
---|---|
OffsetDate | A new OffsetDate for the same date, but with the specified UTC offset. |
Sample snippet
using NodaTime;
using System;
var original = new OffsetDate(
new LocalDate(2019, 5, 3),
Offset.FromHours(3));
OffsetDate updated = original.WithOffset(Offset.FromHours(-3));
Console.WriteLine(updated.Offset);
Console.WriteLine(updated.Date);
Output:
-03
Friday, 03 May 2019
Operators
operator ==(OffsetDate, OffsetDate)
Implements the operator == (equality).
See the type documentation for a description of equality semantics.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator ==(OffsetDate left, OffsetDate right)
Parameters
Type | Name | Description |
---|---|---|
OffsetDate | left | The left hand side of the operator. |
OffsetDate | right | The right hand side of the operator. |
Returns
Type | Description |
---|---|
bool | true if values are equal to each other, otherwise false . |
operator !=(OffsetDate, OffsetDate)
Implements the operator != (inequality).
See the type documentation for a description of equality semantics.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator !=(OffsetDate left, OffsetDate right)
Parameters
Type | Name | Description |
---|---|---|
OffsetDate | left | The left hand side of the operator. |
OffsetDate | right | The right hand side of the operator. |
Returns
Type | Description |
---|---|
bool | true if values are not equal to each other, otherwise false . |
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
Type | Description |
---|---|
XmlSchema | An XmlSchema that describes the XML representation of the object that is produced by the WriteXml(XmlWriter) method and consumed by the ReadXml(XmlReader) method. |
IXmlSerializable.ReadXml(XmlReader)
Generates an object from its XML representation.
Since 2.3.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 2.3.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. |