Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.Now - first and last minutes of the day

Tags:

c#

datetime

Is there any easy way to get a DateTime's "TimeMin" and "TimeMax"?

TimeMin: The very first moment of the day. There is no DateTime that occurs before this one and still occurs on the same day.

TimeMax: The very last moment of the day. There is no DateTime that occurs after this one and still occurs on the same day.

These values would be helpful for filtering and doing date-related queries.

like image 608
Rod Avatar asked Jul 29 '10 13:07

Rod


5 Answers

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());
like image 185
hunter Avatar answered Oct 10 '22 22:10

hunter


I'd use the following:

DateTime now = DateTime.Now;
DateTime startOfDay = now.Date;
DateTime endOfDay = startOfDay.AddDays(1);

and use < endOfDay instead of <= endOfDay. This will mean that it will work regardless of whether the precision is minutes, seconds, milliseconds, ticks, or something else. This will prevent bugs like the one we had on StackOverflow (though the advice was ignored).

Note that it is important to only call DateTime.Now once if you want the start and end of the same day.

like image 32
Mark Byers Avatar answered Oct 10 '22 22:10

Mark Byers


try

//midnight this morning
DateTime timeMin = DateTime.Now.Date; 
//one tick before midnight tonight
DateTime timeMax = DateTime.Now.Date.AddDays(1).AddTicks(-1) 

If you are using this for filtering, as your comments suggest, it is probably a good idea to save DateTime.Now into a variable, just in case the date ticks over between the two calls. Very unlikely but call it enough times and it will inevitably happen one day (night rather).

DateTime currentDateTime = DateTime.Now;
DateTime timeMin = currentDateTime.Date; 
DateTime timeMax = currentDateTime.Date.AddDays(1).AddTicks(-1)
like image 30
Daniel Dyson Avatar answered Oct 10 '22 23:10

Daniel Dyson


One small tweak to hunter's solution above... I use the following extension method to get the end of the day:

public static DateTime EndOfDay(this DateTime input) {
    return input.Date == DateTime.MinValue.Date ? input.Date.AddDays(1).AddTicks(-1) : input.Date.AddTicks(-1).AddDays(1);
}

This should handle cases where the DateTime is either DateTime.MinValue or DateTime.MaxValue. If you call AddDays(1) on DateTime.MaxValue, you will get an exception. Similarly, calling AddTicks(-1) on DateTime.MinValue will also throw an exception.

like image 30
dgelormini Avatar answered Oct 11 '22 00:10

dgelormini


You must be careful to use

(new DateTime()).AddDays(1).AddTicks(-1);

when it is passed to stored procedure.

It could happen that the value will be approximated to next day.

like image 35
gimol79 Avatar answered Oct 10 '22 22:10

gimol79