Table of Contents

Class XDateOnly

Extension methods for the DateOnly class.

Inheritance
System.Object
XDateOnly
Namespace: Galaxon.Core.Time
Assembly: Galaxon.Core.dll
Syntax
public static class XDateOnly : object

Methods

Add(DateOnly, TimeOnly)

Add a time of day to a date to find a new DateTime.

Declaration
public static DateTime Add(this DateOnly date, TimeOnly time)
Parameters
Type Name Description
DateOnly date

The date.

TimeOnly time

The time of day to add.

Returns
Type Description
DateTime

The resulting DateTime.

| Improve this Doc View Source

Add(DateOnly, TimeSpan)

Add a period of time to a date to find a new DateTime.

Declaration
public static DateTime Add(this DateOnly date, TimeSpan period)
Parameters
Type Name Description
DateOnly date

The date.

TimeSpan period

The time period to add.

Returns
Type Description
DateTime

The resulting DateTime.

| Improve this Doc View Source

AddWeeks(DateOnly, Int32)

Add a number of weeks to a date.

Declaration
public static DateOnly AddWeeks(this DateOnly date, int weeks)
Parameters
Type Name Description
DateOnly date

The date.

System.Int32 weeks

The number of weeks to add.

Returns
Type Description
DateOnly

The resulting date.

| Improve this Doc View Source

FromDayOfYear(Int32, Int32)

Find a date given a year and the day of the year.

Formula from Meeus (Astronomical Algorithms 2 ed. p66).

Declaration
public static DateOnly FromDayOfYear(int year, int dayOfYear)
Parameters
Type Name Description
System.Int32 year

The year (1..9999).

System.Int32 dayOfYear

The day of the year (1..366).

Returns
Type Description
DateOnly

The resulting date.

| Improve this Doc View Source

FromJulianDay(Double)

Convert a Julian Day value to a date.

Declaration
public static DateOnly FromJulianDay(double jd)
Parameters
Type Name Description
System.Double jd

The Julian Day value. If a fractional part indicating the time of day is included, this information will be discarded.

Returns
Type Description
DateOnly

A new DateOnly object.

| Improve this Doc View Source

FromTotalDays(Int64)

Find a date given the number of days from the start of the epoch.

Declaration
public static DateOnly FromTotalDays(long days)
Parameters
Type Name Description
System.Int64 days

The number of days.

Returns
Type Description
DateOnly

The resulting date.

| Improve this Doc View Source

FromTotalYears(Double)

Find the date given the number of years since the start of the epoch.

Declaration
public static DateOnly FromTotalYears(double years)
Parameters
Type Name Description
System.Double years

The number of years. May include a fractional part.

Returns
Type Description
DateOnly

The resulting date.

| Improve this Doc View Source

GetChristmas(Int32)

Get the date of Christmas Day in the given year.

Declaration
public static DateOnly GetChristmas(int year)
Parameters
Type Name Description
System.Int32 year

The year.

Returns
Type Description
DateOnly

The date of Christmas in the given year.

| Improve this Doc View Source

GetEaster(Int32)

Get the date of Easter Sunday in the given year. Formula is from Wikipedia. This method uses the "Meeus/Jones/Butcher" algorithm from 1876, with the New Scientist modifications from 1961. Tested for years 1600..2299.

Declaration
public static DateOnly GetEaster(int year)
Parameters
Type Name Description
System.Int32 year

The Gregorian year number.

Returns
Type Description
DateOnly

The date of Easter Sunday for the given year.

| Improve this Doc View Source

GetNthWeekdayInMonth(Int32, Int32, Int32, DayOfWeek)

Find the nth weekday in a given month. Example:

// Get the 4th Thursday in January, 2023.
DateOnly meetup = XDateOnly.GetNthWeekdayInMonth(2023, 1, 4, DayOfWeek.Thursday);

A negative value for n means count from the end of the month. n = -1 means the last one in the month. n = -2 means the second-last, etc. Example:

// Get the last Monday in November, 2025.
DateOnly meetup = XDateOnly.GetNthWeekdayInMonth(2025, 11, -1, DayOfWeek.Monday);
Declaration
public static DateOnly GetNthWeekdayInMonth(int year, int month, int n, DayOfWeek dayOfWeek)
Parameters
Type Name Description
System.Int32 year

The year.

System.Int32 month

The month.

System.Int32 n

Which occurence of the day of the week within the month.

DayOfWeek dayOfWeek

The day of the week.

Returns
Type Description
DateOnly

The requested date.

| Improve this Doc View Source

GetThanksgiving(Int32, String)

Find the date of Thanksgiving for a specified country in a given year. Only years with a holiday called "Thanksgiving" are supported. The default is "US".

Declaration
public static DateOnly GetThanksgiving(int year, string countryCode = "US")
Parameters
Type Name Description
System.Int32 year

The year.

System.String countryCode

The ISO 2-letter country code.

Returns
Type Description
DateOnly

The date of Thanksgiving.

Exceptions
Type Condition
ArgumentInvalidException

Either this country doesn't celebrate Thanksgiving, or the method doesn't support it.

| Improve this Doc View Source

GetTicks(DateOnly)

Get the number of ticks between the start of the epoch (0001-01-01 00:00:00) and the start of the date.

If extension properties are added to the language I may change this to a property "Ticks" later, for consistency with DateTime.

Declaration
public static long GetTicks(this DateOnly date)
Parameters
Type Name Description
DateOnly date

The DateOnly instance.

Returns
Type Description
System.Int64

The number of ticks.

| Improve this Doc View Source

GetTotalDays(DateOnly)

Get the number of days between the start of the epoch and the given date.

Declaration
public static long GetTotalDays(this DateOnly date)
Parameters
Type Name Description
DateOnly date

The DateOnly instance.

Returns
Type Description
System.Int64

The number of days since the epoch start.

| Improve this Doc View Source

GetTotalSeconds(DateOnly)

Get the number of seconds between the start of the epoch and the start of the date.

Declaration
public static long GetTotalSeconds(this DateOnly date)
Parameters
Type Name Description
DateOnly date

The DateOnly instance.

Returns
Type Description
System.Int64

The number of seconds since the epoch start.

| Improve this Doc View Source

GetTotalYears(DateOnly)

Get the number of years between the start of the epoch and the start of the date.

The result will be greater than or equal to date.Year - 1 and less than date.Year.

Declaration
public static double GetTotalYears(this DateOnly date)
Parameters
Type Name Description
DateOnly date

The DateOnly instance.

Returns
Type Description
System.Double

The number of years since the epoch start.

| Improve this Doc View Source

Subtract(DateOnly, DateOnly)

Returns the difference between two dates as a number of days. Emulates the method.

If the end date is later than the start date, the result will be positive. If they are equal, the result will be zero. Otherwise, the result will be negative.

Declaration
public static long Subtract(this DateOnly end, DateOnly start)
Parameters
Type Name Description
DateOnly end

The end date.

DateOnly start

The start date.

Returns
Type Description
System.Int64

The number of days difference between the two dates.

| Improve this Doc View Source

ToDateTime(DateOnly)

Convert a DateOnly to a DateTime, with default time 00:00:00.

Declaration
public static DateTime ToDateTime(this DateOnly date)
Parameters
Type Name Description
DateOnly date

The DateOnly instance.

Returns
Type Description
DateTime

The new DateTime object

| Improve this Doc View Source

ToDateTime(DateOnly, DateTimeKind)

Convert a DateOnly to a DateTime, with default time 00:00:00 and specified DateTimeKind.

Declaration
public static DateTime ToDateTime(this DateOnly date, DateTimeKind kind)
Parameters
Type Name Description
DateOnly date

The DateOnly instance.

DateTimeKind kind

The DateTimeKind.

Returns
Type Description
DateTime

The new DateTime object

| Improve this Doc View Source

ToIsoString(DateOnly)

Format the date using ISO 8601 format YYYY-MM-DD. https://en.wikipedia.org/wiki/ISO_8601#Calendar_dates

Declaration
public static string ToIsoString(this DateOnly date)
Parameters
Type Name Description
DateOnly date

The DateOnly instance.

Returns
Type Description
System.String

A string representing the date in ISO format.

| Improve this Doc View Source

ToJulianDay(DateOnly)

Convert a DateOnly object to a Julian Day value. The result gives the Julian Day at the start of the given date (00:00:00, i.e. midnight), which will always have a fraction of 0.5, since a Julian Day starts at 12:00:00 (noon). ToJulianDay(DateTime)

Declaration
public static double ToJulianDay(this DateOnly date)
Parameters
Type Name Description
DateOnly date

The DateOnly instance.

Returns
Type Description
System.Double

The Julian Day value.

| Improve this Doc View Source
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX | Galaxy logo by Freepik