Discussion on the acquisition of zero point of the day by 12 hour system and 24 hour system

  • 2020-07-21 07:29:46
  • OfStack

Most recently, when writing a timer service, you want to get the zero of the day, but this is how you get it

DateTime dt = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")+" 00:00:00");
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));

Local, test, QA environment tested no problem, but after the public network server, the timing service, there is a problem; When I wrote down the log, I found that the zero point of the day was obtained, and the time obtained after putting it together was the zero point of the previous day. The original server time was 12 hours, and the local environment was 24 hours.

Digging deeper, we found that the period from 00:00:00 to 00:59:59 in the 24-hour period is the date of the day; But during the 12-hour system, it was 12:00:00 to 12:59:59 on the day before yesterday. For example, today's "2013-09-26 12:00:00" according to the 24-hour system, in the 12-hour system, the acquired date is "2013-09-25 12:00:00", DateTime.Now.ToString (" ES12en-ES13en-ES14en "), the obtained date is "2013-09-25", then dt acquired is "2013-09-25 12:00:00".

It turns out that you can get the time this way, depending on the hour system in C#

//24 Hourly:  
DateTime dt = DateTime.Now;
string dt24 = dt.ToString("yyyy-MM-dd HH:mm:ss");
//12 Hourly:  
DateTime dt = DateTime.Now;
string dt12 = dt.ToString("yyyy-MM-dd hh:mm:ss");
// Is made up of H Case determined by 

Regardless of whether the server's time system is 12 hours or 24 hours, uppercase H gets 24 hours and lowercase h gets 12 hours.

Related articles: