Create a date time with month and day only, no year

Well, you can create your own type – but a DateTime always has a full date and time. You can’t even have “just a date” using DateTime – the closest you can come is to have a DateTime at midnight.

You could always ignore the year though – or take the current year:

// Consider whether you want DateTime.UtcNow.Year instead
DateTime value = new DateTime(DateTime.Now.Year, month, day);

To create your own type, you could always just embed a DateTime within a struct, and proxy on calls like AddDays etc:

public struct MonthDay : IEquatable<MonthDay>
{
    private readonly DateTime dateTime;

    public MonthDay(int month, int day)
    {
        dateTime = new DateTime(2000, month, day);
    }

    public MonthDay AddDays(int days)
    {
        DateTime added = dateTime.AddDays(days);
        return new MonthDay(added.Month, added.Day);
    }

    // TODO: Implement interfaces, equality etc
}

Note that the year you choose affects the behaviour of the type – should Feb 29th be a valid month/day value or not? It depends on the year…

Personally I don’t think I would create a type for this – instead I’d have a method to return “the next time the program should be run”.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)