C # datetime.tostring () converted to a uniform format

Source: Internet
Author: User

DateTime.ToString () conversion results are based on the current computer display format to convert, can not agree, and some formats we want to convert them to time, will be error, such as:

2017/11/21/Tuesday 10:23:57, if the conversion of this string is datetime, it will fail, so it is necessary to set a default conversion mode for the program, each time when calling DateTime.ToString (), it is converted directly to the format we want, as follows:

DateTime.ToString ("Yyyy-mm-dd HH:mm:ss")

But this method sometimes feel too troublesome, each need to write, there is no way to write, direct conversion, there, the method is as follows:

Thread.currentthread.currentculture=new CultureInfo ("ZH-CN", True)
{
DateTimeFormat = {ShortDatePattern = "yyyy-mm-dd", Fulldatetimepattern = "Yyyy-mm-dd HH:mm:ss", Longtimepattern = "HH:mm: SS "}
};

Just add the sentence when you initialize it.

C # datetime.tostring () converted to a uniform format

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.