94

I need to create a midnight DateTime

I've just done this:

DateTime endTime = DateTime.Now;
endTime.Subtract(endTime.TimeOfDay);

Haven't test it yet, I'm assuming it works but is there a better/cleaner way?

7 Answers 7

193

Just use foo.Date, or DateTime.Today for today's date

3
  • 4
    It is worth considering that people generally think of midnight as the last second of the day, whereas this gives the 1st second of the day (which is a different midnight). So if you intend to accomplish the popular meaning of midnight, please see Aruna's answer below (and Paul Suart's comment)
    – trajekolus
    Commented Jan 24, 2020 at 0:16
  • @jdoer1997 - It depends what your app needs to do at midnight. Apps that depend on date, day-of-week, etc. may want to do house keeping tasks at the first tick into the new day.
    – LT Dan
    Commented Feb 20, 2020 at 13:03
  • If you are dealing with multiple time zones you may want to be careful with DateTime.Today. In this case DateTime.UtcNow.Date might have more desirable results. Commented Oct 9, 2023 at 8:08
27
DateTime endTime = DateTime.Now.Date;

Now endTime.TimeOfDay.ToString() returns "00:00:00"

20

DateTime.Now . AddDays(1) . Date

1
  • 18
    I think DateTime.Today.AddDays(1) is nicer than this. Commented Jan 5, 2016 at 15:23
17

DateTime.Today

16

You can use DateTime.Today with exact seconds of the midnight.

    DateTime today = DateTime.Today;
    DateTime mid = today.AddDays(1).AddSeconds(-1);
    Console.WriteLine(string.Format("Today: {0} , Mid Night: {1}", today.ToString(), mid.ToString()));

    Console.ReadLine();

This should print :

Today: 11/24/2016 10:00:00 AM , Mid Night: 11/24/2016 11:59:59 PM
2
  • This helped a lot and it is exactly what i wanted Commented Feb 9, 2018 at 8:49
  • 11
    Midnight is the first one - it's the very first second of the day, not the last.
    – Paul Suart
    Commented Mar 1, 2018 at 9:00
1
var dateMidnight = DateTime.ParseExact(DateTime.Now.ToString("yyyyMMdd"), "yyyyMMdd", CultureInfo.InvariantCulture);
1
  • 4
    In addition to providing some code, please add some additional details about why your solution works and how it differs from previous solutions.
    – wlh
    Commented Jan 22, 2019 at 20:49
-3
    private bool IsServiceDatabaseProcessReadyToStart()
    {
        bool isGoodParms = true;
        DateTime currentTime = DateTime.Now;
        //24 Hour Clock
        string[] timeSpan = currentTime.ToString("HH:mm:ss").Split(':');
        //Default to Noon
        int hr = 12;
        int mn = 0;
        int sc = 0;

        if (!string.IsNullOrEmpty(timeSpan[0]))
        {
            hr = Convert.ToInt32(timeSpan[0]);
        }
        else
        {
            isGoodParms = false;
        }

        if (!string.IsNullOrEmpty(timeSpan[1]))
        {
            mn = Convert.ToInt32(timeSpan[1]);
        }
        else
        {
            isGoodParms = false;
        }

        if (!string.IsNullOrEmpty(timeSpan[2]))
        {
            sc = Convert.ToInt32(timeSpan[2]);
        }
        else
        {
            isGoodParms = false;
        }

        if (isGoodParms == true )
        {
            TimeSpan currentTimeSpan = new TimeSpan(hr, mn, sc);
            TimeSpan minTimeSpan = new TimeSpan(0, 0, 0);
            TimeSpan maxTimeSpan = new TimeSpan(0, 04, 59);
            if (currentTimeSpan >= minTimeSpan && currentTimeSpan <= maxTimeSpan)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
1
  • 1
    Welcome to StackOverflow. In addition to providing some code, please provide some additional details about why your solution works and how it differs from previous solutions.
    – buczek
    Commented Jan 24, 2017 at 20:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.