Best way to create a Midnight DateTime in C#

C#Datetime

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

DateTime endTime = DateTime.Now.Date;

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

Solution 3 - C#

DateTime.Today

Solution 4 - C#

Solution 5 - C#

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

Solution 6 - C#

var dateMidnight = DateTime.ParseExact(DateTime.Now.ToString("yyyyMMdd"), "yyyyMMdd", CultureInfo.InvariantCulture);

Solution 7 - C#

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

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
QuestionendianView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow
Solution 2 - C#zendarView Answer on Stackoverflow
Solution 3 - C#mmiikaView Answer on Stackoverflow
Solution 4 - C#WebDudeView Answer on Stackoverflow
Solution 5 - C#ArunaView Answer on Stackoverflow
Solution 6 - C#PeterView Answer on Stackoverflow
Solution 7 - C#David PetersenView Answer on Stackoverflow