How can I specify the latest time of day with DateTime

.NetDatetime

.Net Problem Overview


I am using a System.DateTime object to allow a user to select a date range. The user is only able to select a date (not time) using a third party calendar so I will need to automatically specify the time of day it should use (ie: 00:00:00 or 23:59:59) after the date is chosen.

How can I specify the time after the date is already stored as a DateTime object by the calendar selector? I could use the AddHours, AddMinutes, AddSeconds methods but those are relative to the current time which may not be 00:00:00.

The startDate will need to have a time of 00:00:00 and endDate have a time of 23:59:59 to account for the entire days.

.Net Solutions


Solution 1 - .Net

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);

Solution 2 - .Net

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

Solution 3 - .Net

DateTime d = DateTime.Today.AddDays(1).AddTicks(-1);

Solution 4 - .Net

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.

Solution 5 - .Net

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);
    }
}

Solution 6 - .Net

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.

Solution 7 - .Net

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.

Solution 8 - .Net

For example

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

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

Solution 9 - .Net

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.

Solution 10 - .Net

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

Solution 11 - .Net

use this

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

Solution 12 - .Net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoe PhillipsView Question on Stackoverflow
Solution 1 - .NetJose BasilioView Answer on Stackoverflow
Solution 2 - .NetJon BView Answer on Stackoverflow
Solution 3 - .NetPeter DrierView Answer on Stackoverflow
Solution 4 - .NetJoeView Answer on Stackoverflow
Solution 5 - .NetHein Andre GrønnestadView Answer on Stackoverflow
Solution 6 - .NetErich MirabalView Answer on Stackoverflow
Solution 7 - .NetKenView Answer on Stackoverflow
Solution 8 - .Net0x49D1View Answer on Stackoverflow
Solution 9 - .NetakdView Answer on Stackoverflow
Solution 10 - .NetdunwanView Answer on Stackoverflow
Solution 11 - .NetMohammad Javad TavakoliView Answer on Stackoverflow
Solution 12 - .NetMd. Tazbir Ur Rahman BhuiyanView Answer on Stackoverflow