Best way to create a Midnight DateTime in C#

前端 未结 7 1113
南旧
南旧 2020-12-29 01:00

I need to create a midnight DateTime

I\'ve just done this:

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

Ha

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 01:23

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

提交回复
热议问题