ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Fluent DateOnly Extensions

A fluent way to create DateOnly values.

Source

static class DateOnlyExtensions {

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month January for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    public static DateOnly January(this int day) {
        return new DateOnly(DateTime.Today.Year, 1, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month January.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly January(this int day, int year) {
        return new DateOnly(year, 1, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month February for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 29)" />).</param>
    public static DateOnly February(this int day) {
        return new DateOnly(DateTime.Today.Year, 2, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month February.
    /// </summary>
    /// <param name="day">The day (1 through the 29)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly February(this int day, int year) {
        return new DateOnly(year, 2, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month March for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    public static DateOnly March(this int day) {
        return new DateOnly(DateTime.Today.Year, 3, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month March.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly March(this int day, int year) {
        return new DateOnly(year, 3, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month April for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 30)" />).</param>
    public static DateOnly April(this int day) {
        return new DateOnly(DateTime.Today.Year, 4, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month April.
    /// </summary>
    /// <param name="day">The day (1 through the 30)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly April(this int day, int year) {
        return new DateOnly(year, 4, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month May for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    public static DateOnly May(this int day) {
        return new DateOnly(DateTime.Today.Year, 5, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month May.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly May(this int day, int year) {
        return new DateOnly(year, 5, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month June for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 30)" />).</param>
    public static DateOnly June(this int day) {
        return new DateOnly(DateTime.Today.Year, 6, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month June.
    /// </summary>
    /// <param name="day">The day (1 through the 30)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly June(this int day, int year) {
        return new DateOnly(year, 6, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month July for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    public static DateOnly July(this int day) {
        return new DateOnly(DateTime.Today.Year, 7, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month July.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly July(this int day, int year) {
        return new DateOnly(year, 7, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month August for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    public static DateOnly August(this int day) {
        return new DateOnly(DateTime.Today.Year, 8, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month August.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly August(this int day, int year) {
        return new DateOnly(year, 8, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month September for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 30)" />).</param>
    public static DateOnly September(this int day) {
        return new DateOnly(DateTime.Today.Year, 9, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month September.
    /// </summary>
    /// <param name="day">The day (1 through the 30)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly September(this int day, int year) {
        return new DateOnly(year, 9, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month October for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    public static DateOnly October(this int day) {
        return new DateOnly(DateTime.Today.Year, 10, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month October.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly October(this int day, int year) {
        return new DateOnly(year, 10, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month November for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 30)" />).</param>
    public static DateOnly November(this int day) {
        return new DateOnly(DateTime.Today.Year, 11, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month November.
    /// </summary>
    /// <param name="day">The day (1 through the 30)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly November(this int day, int year) {
        return new DateOnly(year, 11, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> 
    /// in the month December for this year.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    public static DateOnly December(this int day) {
        return new DateOnly(DateTime.Today.Year, 12, day);
    }

    /// <summary>
    /// Returns a new <see cref="DateOnly"/> value for the specified <paramref name="day"/> and <paramref name="year"/>
    /// in the month December.
    /// </summary>
    /// <param name="day">The day (1 through the 31)" />).</param>
    /// <param name="year">The year (1 through 9999).</param>
    public static DateOnly December(this int day, int year) {
        return new DateOnly(year, 12, day);
    }
}

Example

Console.WriteLine(1.April(2023));     // 4/1/2023
Console.WriteLine(25.December());     // 12/25/2022 (it is 2022 when I coded this).

Author: Fons Sonnemans

Submitted on: 27 nov. 2022

Language: csharp

Type: System.Int32

Views: 1955