Recently, when writing a scheduled service, you need to obtain the zero point of the current day.
Copy codeThe Code is as follows:
DateTime dt = DateTime. Parse (DateTime. Now. ToString ("yyyy-MM-dd") + "00:00:00 ");
Console. WriteLine (dt. ToString ("yyyy-MM-dd HH: mm: ss "));
In the local environment, the QA environment is tested and there is no problem, but after the public network server is installed, the scheduled service becomes faulty. After the log is written, it is found that the zero point of the day is obtained, the obtained time after the fight is the zero point of the previous day. The original server time is in the 12-hour format, and the local environment is in the 24-hour format.
Continue to study and find that the period from 00:00:00 to 00:59:59 each day is the date of the day in the 24-hour system, but within the 12-hour system, it is the date of the day before yesterday from 12:00:00 to 12:59:59. For example, for a 24-hour server, the time of "00:00:00" today is "12:00:00", DateTime. now. toString ("yyyy-MM-dd") the date obtained by this method is "", and dt is "12:00:00 ".
It turns out that, depending on the hour system of the time obtained in C #, you can obtain
Copy codeThe Code is as follows:
// 24-hour system:
DateTime dt = DateTime. Now;
String dt24 = dt. ToString ("yyyy-MM-dd HH: mm: ss ");
// 12-hour system:
DateTime dt = DateTime. Now;
String dt12 = dt. ToString ("yyyy-MM-dd hh: mm: ss ");
// It is determined by the case sensitivity of H.
Whether the server's time system is 12 hours or 24 hours, uppercase H gets 24 hours, while lowercase h gets 12 hours.