DateTime.Now – first and last minutes of the day

Here are two extensions I use to do exactly that.

    /// <summary>
    /// Gets the 12:00:00 instance of a DateTime
    /// </summary>
    public static DateTime AbsoluteStart(this DateTime dateTime)
    {
        return dateTime.Date;
    }

    /// <summary>
    /// Gets the 11:59:59 instance of a DateTime
    /// </summary>
    public static DateTime AbsoluteEnd(this DateTime dateTime)
    {
        return AbsoluteStart(dateTime).AddDays(1).AddTicks(-1);
    }

This allows you to write:

DateTime.Now.AbsoluteEnd() || DateTime.Now.AbsoluteStart()

or

DateTime partyTime = new DateTime(1999, 12, 31);

Console.WriteLine("Start := " + partyTime.AbsoluteStart().ToString());
Console.WriteLine("End := " + partyTime.AbsoluteEnd().ToString());

Leave a Comment

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