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, netstandard2.0
Inherited Members
Object.Equals(Object, Object)
Object.GetType()
Object.ReferenceEquals(Object, Object)
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
[TypeConverter(typeof(YearMonthTypeConverter))]
public struct YearMonth : IEquatable<YearMonth>, IComparable<YearMonth>, IComparable, IFormattable, IXmlSerializable
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 System.ArgumentException; attempting to compare values in different calendars almost always indicates a bug in the calling code.
Constructors
YearMonth(Era, Int32, Int32)
Constructs an instance for the given era, year of era and month in the ISO calendar.
Since 3.0.x
Availability net6.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. |
Int32 | yearOfEra | The year of era. |
Int32 | 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, Int32, Int32, CalendarSystem)
Constructs an instance for the given era, year of era and month in the specified calendar.
Since 3.0.x
Availability net6.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. |
Int32 | yearOfEra | The year of era. |
Int32 | 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(Int32, Int32)
Constructs an instance for the given year and month in the ISO calendar.
Since 3.0.x
Availability net6.0, netstandard2.0
Declaration
public YearMonth(int year, int month)
Parameters
Type | Name | Description |
---|---|---|
Int32 | year | The year. This is the "absolute year", so a value of 0 means 1 BC, for example. |
Int32 | 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(Int32, Int32, CalendarSystem)
Constructs an instance for the given year and month in the specified calendar.
Since 3.0.x
Availability net6.0, netstandard2.0
Declaration
public YearMonth(int year, int month, CalendarSystem calendar)
Parameters
Type | Name | Description |
---|---|---|
Int32 | year | The year. This is the "absolute year", so, for the ISO calendar, a value of 0 means 1 BC, for example. |
Int32 | 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, netstandard2.0
Declaration
public readonly 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, netstandard2.0
Declaration
public readonly 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, netstandard2.0
Declaration
public readonly int Month { get; }
Property Value
Type | Description |
---|---|
Int32 | The month of this year/month within the year. |
Year
Gets the year of this year/month.
Since 3.0.x
Availability net6.0, netstandard2.0
Declaration
public readonly int Year { get; }
Property Value
Type | Description |
---|---|
Int32 | 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, netstandard2.0
Declaration
public readonly int YearOfEra { get; }
Property Value
Type | Description |
---|---|
Int32 | 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, 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 . |
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, 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 |
---|---|
Int32 | 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, 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 |
---|---|
Boolean | 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, 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 |
---|---|
Boolean | True if the given value is another year/month equal to this one; false otherwise. |
Overrides
System.ValueType.Equals(System.Object)
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, netstandard2.0
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 | A hash code for this value. |
Overrides
System.ValueType.GetHashCode()
OnDayOfMonth(Int32)
Returns a LocalDate with the year/month of this value, and the given day of month.
Since 3.0.x
Availability net6.0, netstandard2.0
Declaration
public LocalDate OnDayOfMonth(int day)
Parameters
Type | Name | Description |
---|---|---|
Int32 | 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(Int32)
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, netstandard2.0
Declaration
public YearMonth PlusMonths(int months)
Parameters
Type | Name | Description |
---|---|---|
Int32 | 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, 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 System.String that represents this instance.
Since 3.1.x
Availability net6.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
System.ValueType.ToString()
ToString(String, IFormatProvider)
Formats the value of the current instance using the specified pattern.
Since 3.0.x
Availability net6.0, netstandard2.0
Declaration
public 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 ISO 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. |
Remarks
Unlike most System.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
Equality(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, 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 |
---|---|
Boolean | True if the two year/month values are the same and in the same calendar; false otherwise |
GreaterThan(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, 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 |
---|---|
Boolean | 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 . |
GreaterThanOrEqual(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, 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 |
---|---|
Boolean | 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 . |
Inequality(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, 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 |
---|---|
Boolean | False if the two year/month values are the same and in the same calendar; true otherwise |
LessThan(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, 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 |
---|---|
Boolean | 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 . |
LessThanOrEqual(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, 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 |
---|---|
Boolean | 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 System.IComparable.CompareTo(System.Object) to compare two YearMonth values.
See the type documentation for a description of ordering semantics.
Since 3.0.x
Availability net6.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 |
---|---|
Int32 | 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.ReadXml(XmlReader)
Since 3.0.x
Availability net6.0, netstandard2.0
Declaration
void IXmlSerializable.ReadXml(XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
XmlReader | reader |
IXmlSerializable.WriteXml(XmlWriter)
Since 3.0.x
Availability net6.0, netstandard2.0
Declaration
void IXmlSerializable.WriteXml(XmlWriter writer)
Parameters
Type | Name | Description |
---|---|---|
XmlWriter | writer |
Implements
System.IEquatable<T>
System.IComparable<T>
System.IComparable
System.IFormattable
System.Xml.Serialization.IXmlSerializable