Reputation: 15455
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.
Upvotes: 51
Views: 51019
Reputation: 742
DateTime.Today.AddDays(1).AddSeconds(-1);
Not very exact, but fixed my problems. Now we can use AddMilliseconds, AddTicks and etc. I think it will just vary on what would satisfy your need.
Upvotes: 0
Reputation: 21
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.
Upvotes: 2
Reputation: 63522
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());
Upvotes: 120
Reputation: 15664
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
Upvotes: 1
Reputation: 113
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.
Upvotes: 3
Reputation: 209
public static DateTime ToEndOfDay(this DateTime time)
{
var endOfDaySpan = TimeSpan.FromDays(1).Subtract(TimeSpan.FromMilliseconds(1));
return time.Date.Add(endOfDaySpan);
}
Upvotes: 1
Reputation: 2539
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.
Upvotes: 1
Reputation: 13230
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)
Upvotes: 11
Reputation: 9543
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.
Upvotes: 1
Reputation: 838276
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.
Upvotes: 15