12-hour and 24-hour acquisition at on the current day

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.