DateTime.Now - first and last minutes of the day

0 votes
asked Jul 29, 2010 by rod

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

9 Answers

0 votes
answered Jul 29, 2010 by hunter

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());
0 votes
answered Jul 29, 2010 by daniel-dyson

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)
0 votes
answered Jul 29, 2010 by mark-byers

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.

0 votes
answered Jul 29, 2010 by martin-neal

Like other answerers, I'm not quite sure what you're asking for, but incase you want the smallest possible time and the largest possible time, (not just in a day), then there's DateTime.MinValue and DateTime.MaxValue which return 1/1/0001 12:00:00 AM and 12/31/9999 11:59:59 PM respectively.

0 votes
answered Jul 7, 2012 by mir

I would advise that you look at this answer: How can I specify the latest time of day with DateTime

If your original DateTimes also potentially include times, using the AddDays() method will add a full 24 hours, which may not be precisely what you want.

0 votes
answered Jan 30, 2013 by dshook

Please note that if you're passing this time to sql server you should use

dateTime.Date.AddDays(1).AddMilliseconds(-3);

See:

How do I get the last possible time of a particular day

0 votes
answered Jul 9, 2013 by zoltanflavius
    public static DateTime ToEndOfDay(this DateTime time)
    {
        var endOfDaySpan = TimeSpan.FromDays(1).Subtract(TimeSpan.FromMilliseconds(1));
        return time.Date.Add(endOfDaySpan);
    }
0 votes
answered Jul 15, 2013 by dgelormini

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.

0 votes
answered Jul 13, 2016 by gimol79

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...