如何使用 DateTime 指定一天中的最新时间

我使用一个 System.DateTime对象来允许用户选择一个日期范围。用户只能选择一个日期(而不是时间)使用第三方日历,所以我需要自动指定它应该使用的日期(即: 00:00:00或23:59:59)后,日期选择。

如何指定日期已被日历选择器存储为 DateTime 对象之后的时间?我可以使用 AddHours, AddMinutes, AddSeconds方法,但那些是相对于当前的时间,可能不是00:00:00。

startDate需要00:00:00的时间,而 endDate需要23:59:59的时间来计算整个天数。

71537 次浏览

If you already have a DateTime object created and want to replace the time with the 11:59:59PM for that given date, then you can use the .Date property to get the date with time set to 00:00:00 and then add the hours, minutes and seconds. For example:

var dt = yourDateInstance.Date.AddHours(23).AddMinutes(59).AddSeconds(59);

If by latest time, you mean 11:59:59 PM, then this should also work:

var dt = new DateTime(Now.Year, Now.Month, Now.Day, 23, 59, 59);

To get the last instant for today:

DateTime d = new DateTime(Now.Year, Now.Month, Now.Day);
d = d.AddDays(1);
d = d.AddTicks(-1);

In response to your edit, here's what I would do:

DateTime start = new DateTime(Now.Year, Now.Month, Now.Day);
DateTime end = start.AddDays(1).AddTicks(-1);


// Or - just use end = start.AddDays(1), and use a < for comparison
DateTime d = DateTime.Today.AddDays(1).AddTicks(-1);
DateTime startDate = DateTime.Today;
DateTime stopDate = startDate.AddDays(1).AddTicks(-1);

As a note, DateTime.Today returns (from MSDN)

A System.DateTime set to today's date, with the time component set to 00:00:00.

So as others have noted, add a day, then subtract the smallest time quantum (a tick), and you get the last possible time for the current day.

Of course, you might have to think about TimeZones and such depending where the code runs versus where the user is. UTC time might be good, but that might bump you off a day (either way) depending where your code runs.

Your question has already been answered, but IMHO a better way is not to bother attempting to subtract a tick, a second, or whatever from the end of the range, and compare using strictly less than.

So that if you want all dates in an inclusive range from startDate to endDate, ignoring the time, you could use the following in C#:

if ((myDate >= startDate.Date) && (myDate < endDate.Date.AddDays(1)))
{
// ... it's in the range
}

or in T-SQL, assuming your @StartDate and @EndDate are exactly midnight, something like:

WHERE SomeDate >= @StartDate AND SomeDate < DATEADD(d,1,@EndDate)

UPDATE

Updated example to show an inclusive range in response to comments.

use this

DateTime YourNewDate = new DateTime(YourDate .Year, YourDate .Month, YourDate .Day, 23, 59, 59, 99);

yourDateInstance.CloseDate = yourDateInstance.CloseDate.Date.AddDays(1).AddMilliseconds(-1);

For example

DateTime.Now.Date.AddDays(1).AddSeconds(-1)

Or AddTicks/AddMilliseconds/AddMinutes... based on the precision you need.

Based on the other answers I created this convenient extension method:

public static class DateTimeExtensions
{
public static DateTime EndOfDay(this DateTime dateTime)
{
return dateTime.Date.AddDays(1).AddTicks(-1);
}
}

this will give you the expected result:

DateTime startDate= DateTime.Now.Date;
DateTime endDate= startDate.AddDays(2).Add(new TimeSpan(23, 59, 59));
//startDate: 28/9/2017 0:0:0 endDate: 29/9/2017 23:59:59

Why not ToDayEnd() extension

/// <summary>
/// Gets the value of the End of the day (23:59)
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
public static DateTime ToDayEnd(this DateTime target)
{
return target.Date.AddDays(1).AddMilliseconds(-1);
}

But if you would really mean the absolute end of the day then AddTicks(-1) is the answer.

Using an Extension Method

public static DateTime EndOfTheDay(this DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day).AddDays(1).AddTicks(-1);
}

The result here would provide you with the latest time possible by getting the beginning of the day - add a day and then subtract one tick. Other methods add Hours, Minutes and Seconds however those solutions depending on code functions will cause issues for any time between 23:59:59.000000 and 23:59:59.999999

For example if I want to know if a value is before a certain end date / time , the possibility with other solutions is that they would miss values in the millisecond range.

var startDate = UIModel.StartDate; var endDate = UIModel.EndDate.AddDays(1);

SQL query: ... WHERE date >= @startDate AND date < @endDate

  • so don't include "euqal" comparison for end date in the SQL query.

I think is better to use an Extention method to get start time of day or end time of day everywhere.

public static class ExtensionMethod
{
public static DateTime GetEndTimeOfDateTime(this DateTime datetime)
{
if(datetime==null) return datetime;


return new DateTime(datetime.Year, datetime.Month, datetime.Day, 23, 59, 59);
}


public static DateTime GetStartTimeOfDateTime(this DateTime datetime)
{
if(datetime==null) return datetime;


return new DateTime(datetime.Year, datetime.Month, datetime.Day, 0, 0, 0);
}
}