Struct YearMonth
A year and month in a particular calendar. This is effectively
LocalDate without the day-of-month component.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Implements
Inherited Members
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
[TypeConverter(typeof(YearMonthTypeConverter))]
public struct YearMonth : IEquatable<YearMonth>, IComparable<YearMonth>, IComparable, IFormattable, IXmlSerializable, IComparisonOperators<YearMonth, YearMonth, bool>, IEqualityOperators<YearMonth, YearMonth, bool>
Remarks
Values can freely be compared for equality: a value in a different calendar system is not equal to a value in a different calendar system. However, ordering comparisons fail with ArgumentException; attempting to compare values in different calendars almost always indicates a bug in the calling code.
Constructors
YearMonth(Era, int, int)
Constructs an instance for the given era, year of era and month in the ISO calendar.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public YearMonth(Era era, int yearOfEra, int month)
Parameters
Type | Name | Description |
---|---|---|
Era | era | The era within which to create a year/month. Must be a valid era within the ISO calendar. |
int | yearOfEra | The year of era. |
int | month | The month of year. |
Sample snippet
using NodaTime;
using NodaTime.Calendars;
using System;
YearMonth yearMonth = new YearMonth(Era.Common, 1994, 5);
Console.WriteLine(yearMonth.Year);
Console.WriteLine(yearMonth.Month);
Console.WriteLine(yearMonth.Calendar);
Console.WriteLine(yearMonth.Era);
Output:
1994
5
ISO
CE
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | The parameters do not form a valid year/month. |
YearMonth(Era, int, int, CalendarSystem)
Constructs an instance for the given era, year of era and month in the specified calendar.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public YearMonth(Era era, int yearOfEra, int month, CalendarSystem calendar)
Parameters
Type | Name | Description |
---|---|---|
Era | era | The era within which to create a year/month. Must be a valid era within the specified calendar. |
int | yearOfEra | The year of era. |
int | month | The month of year. |
CalendarSystem | calendar | Calendar system in which to create the year/month. |
Sample snippet
using NodaTime;
using NodaTime.Calendars;
using System;
YearMonth yearMonth = new YearMonth(Era.Common, 2019, 5, CalendarSystem.Gregorian);
Console.WriteLine(yearMonth.Year);
Console.WriteLine(yearMonth.Month);
Console.WriteLine(yearMonth.Calendar);
Console.WriteLine(yearMonth.Era);
Output:
2019
5
Gregorian
CE
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | The parameters do not form a valid year/month. |
YearMonth(int, int)
Constructs an instance for the given year and month in the ISO calendar.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public YearMonth(int year, int month)
Parameters
Type | Name | Description |
---|---|---|
int | year | The year. This is the "absolute year", so a value of 0 means 1 BC, for example. |
int | month | The month of year. |
Sample snippet
using NodaTime;
using System;
YearMonth yearMonth = new YearMonth(2019, 5);
Console.WriteLine(yearMonth.Year);
Console.WriteLine(yearMonth.Month);
Console.WriteLine(yearMonth.Calendar);
Output:
2019
5
ISO
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | The parameters do not form a valid year/month. |
YearMonth(int, int, CalendarSystem)
Constructs an instance for the given year and month in the specified calendar.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public YearMonth(int year, int month, CalendarSystem calendar)
Parameters
Type | Name | Description |
---|---|---|
int | year | The year. This is the "absolute year", so, for the ISO calendar, a value of 0 means 1 BC, for example. |
int | month | The month of year. |
CalendarSystem | calendar | Calendar system in which to create the year/month. |
Sample snippet
using NodaTime;
using System;
YearMonth yearMonth = new YearMonth(2014, 3, CalendarSystem.Julian);
Console.WriteLine(yearMonth.Year);
Console.WriteLine(yearMonth.Month);
Console.WriteLine(yearMonth.Calendar);
Output:
2014
3
Julian
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | The parameters do not form a valid year/month. |
Properties
Calendar
Gets the calendar system associated with this year/month.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public CalendarSystem Calendar { get; }
Property Value
Type | Description |
---|---|
CalendarSystem | The calendar system associated with this year/month. |
Era
Gets the era of this year/month.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Era Era { get; }
Property Value
Type | Description |
---|---|
Era | The era of this year/month. |
Month
Gets the month of this year/month within the year.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Month { get; }
Property Value
Type | Description |
---|---|
int | The month of this year/month within the year. |
Year
Gets the year of this year/month.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Year { get; }
Property Value
Type | Description |
---|---|
int | The year of this year/month. |
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 value within the era.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int YearOfEra { get; }
Property Value
Type | Description |
---|---|
int | The year of this value within the era. |
Methods
AddSchema(XmlSchemaSet)
Adds the XML schema type describing the structure of the YearMonth 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 . |
CompareTo(YearMonth)
Indicates whether this year/month is earlier, later or the same as another one.
See the type documentation for a description of ordering semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int CompareTo(YearMonth other)
Parameters
Type | Name | Description |
---|---|---|
YearMonth | other | The other year/month to compare this one with |
Returns
Type | Description |
---|---|
int | A value less than zero if this year/month is earlier than other ;
zero if this year/month is the same as other ; a value greater than zero if this date is
later than other . |
Exceptions
Type | Condition |
---|---|
ArgumentException | The calendar system of other is not the
same as the calendar system of this value. |
Equals(YearMonth)
Compares two YearMonth values for equality.
See the type documentation for a description of ordering semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool Equals(YearMonth other)
Parameters
Type | Name | Description |
---|---|---|
YearMonth | other | The value to compare this year/month with. |
Returns
Type | Description |
---|---|
bool | True if the given value is another year/month equal to this one; false otherwise. |
Equals(object?)
Compares two YearMonth values for equality.
See the type documentation for a description of equality semantics.
Since 3.0.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 year/month with. |
Returns
Type | Description |
---|---|
bool | True if the given value is another year/month equal to this one; false otherwise. |
Overrides
GetHashCode()
Returns a hash code for this year/month.
See the type documentation for a description of equality semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | A hash code for this value. |
Overrides
OnDayOfMonth(int)
Returns a LocalDate with the year/month of this value, and the given day of month.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public LocalDate OnDayOfMonth(int day)
Parameters
Type | Name | Description |
---|---|---|
int | day | The day within the month. |
Returns
Type | Description |
---|---|
LocalDate | The result of combining this year and month with day . |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | day does not fall within the
month represented by this value. |
PlusMonths(int)
Returns a YearMonth object which is the result of adding the specified number
of months to this object.
Since 3.1.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public YearMonth PlusMonths(int months)
Parameters
Type | Name | Description |
---|---|---|
int | months | The number of months to add to this object. |
Returns
Type | Description |
---|---|
YearMonth | The resulting YearMonth after adding the specified number of months. |
ToDateInterval()
Returns a DateInterval covering the month represented by this value.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public DateInterval ToDateInterval()
Returns
Type | Description |
---|---|
DateInterval | A DateInterval covering the month represented by this value. |
Sample snippet
using NodaTime;
using System;
YearMonth yearMonth = new YearMonth(2019, 5);
DateInterval interval = yearMonth.ToDateInterval();
Console.WriteLine(interval.Start);
Console.WriteLine(interval.End);
Output:
Wednesday, 01 May 2019
Friday, 31 May 2019
ToString()
Returns a string that represents this instance.
Since 3.1.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 culture-specific 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 3.0.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 ISO 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
Unlike most IFormattable implementations, a
patternText
of null with
the current thread's culture does not yield the same result as the parameterless ToString()
overload, for backward-compatibility reasons. (It uses the ISO format, which is culture-insensitive.)Operators
operator ==(YearMonth, YearMonth)
Compares two YearMonth values for equality.
See the type documentation for a description of equality semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator ==(YearMonth lhs, YearMonth rhs)
Parameters
Type | Name | Description |
---|---|---|
YearMonth | lhs | The first value to compare |
YearMonth | rhs | The second value to compare |
Returns
Type | Description |
---|---|
bool | True if the two year/month values are the same and in the same calendar; false otherwise |
operator >(YearMonth, YearMonth)
Compares two YearMonth values to see if the left one is strictly later than the right one.
See the type documentation for a description of ordering semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator >(YearMonth lhs, YearMonth rhs)
Parameters
Type | Name | Description |
---|---|---|
YearMonth | lhs | First operand of the comparison |
YearMonth | rhs | Second operand of the comparison |
Returns
Type | Description |
---|---|
bool | true if the lhs is strictly later than rhs , false otherwise. |
Exceptions
Type | Condition |
---|---|
ArgumentException | The calendar system of rhs is not the same
as the calendar of lhs . |
operator >=(YearMonth, YearMonth)
Compares two YearMonth values to see if the left one is later than or equal to the right one.
See the type documentation for a description of ordering semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator >=(YearMonth lhs, YearMonth rhs)
Parameters
Type | Name | Description |
---|---|---|
YearMonth | lhs | First operand of the comparison |
YearMonth | rhs | Second operand of the comparison |
Returns
Type | Description |
---|---|
bool | true if the lhs is later than or equal to rhs , false otherwise. |
Exceptions
Type | Condition |
---|---|
ArgumentException | The calendar system of rhs is not the same
as the calendar of lhs . |
operator !=(YearMonth, YearMonth)
Compares two YearMonth values for inequality.
See the type documentation for a description of equality semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator !=(YearMonth lhs, YearMonth rhs)
Parameters
Type | Name | Description |
---|---|---|
YearMonth | lhs | The first value to compare |
YearMonth | rhs | The second value to compare |
Returns
Type | Description |
---|---|
bool | False if the two year/month values are the same and in the same calendar; true otherwise |
operator <(YearMonth, YearMonth)
Compares two YearMonth values to see if the left one is strictly earlier than the right one.
See the type documentation for a description of ordering semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator <(YearMonth lhs, YearMonth rhs)
Parameters
Type | Name | Description |
---|---|---|
YearMonth | lhs | First operand of the comparison |
YearMonth | rhs | Second operand of the comparison |
Returns
Type | Description |
---|---|
bool | true if the lhs is strictly earlier than rhs , false otherwise. |
Exceptions
Type | Condition |
---|---|
ArgumentException | The calendar system of rhs is not the same
as the calendar of lhs . |
operator <=(YearMonth, YearMonth)
Compares two YearMonth values to see if the left one is earlier than or equal to the right one.
See the type documentation for a description of ordering semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator <=(YearMonth lhs, YearMonth rhs)
Parameters
Type | Name | Description |
---|---|---|
YearMonth | lhs | First operand of the comparison |
YearMonth | rhs | Second operand of the comparison |
Returns
Type | Description |
---|---|
bool | true if the lhs is earlier than or equal to rhs , false otherwise. |
Exceptions
Type | Condition |
---|---|
ArgumentException | The calendar system of rhs is not the same
as the calendar of lhs . |
Explicit Interface Implementations
IComparable.CompareTo(object?)
Implementation of CompareTo(object) to compare two YearMonth values.
See the type documentation for a description of ordering semantics.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
int IComparable.CompareTo(object? obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare this value with. |
Returns
Type | Description |
---|---|
int | The result of comparing this YearMonth with another one.
If obj is null, this method returns a value greater than 0. |
Remarks
This uses explicit interface implementation to avoid it being called accidentally. The generic implementation should usually be preferred.
Exceptions
Type | Condition |
---|---|
ArgumentException | obj is non-null but does not refer to an instance of YearMonth, or refers
to a value in a different calendar system. |
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 3.0.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 3.0.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. |