C # DateTime. ToString generates the time format of different languages,

Source: Internet
Author: User
Tags iso 8601

C # DateTime. ToString generates the time format of different languages,

I think you can use DateTime. the ToString () method must have been very familiar with usage, but I think most of the usage I have used is: DateTime. toString ("format"), but this article will introduce another overload method DateTime. toString ("format", IFormatProvider ).

If you have a multi-language requirement for your project, you will certainly have some functions to display a time. Due to differences in text, users in different languages must have different requirements for time formats, for example, the time format in English is 12/2/2013 8:20:22, while the German format is 02.12.2013 20:20:22. The difference is large, and if there are many languages, it is also difficult to make specific processing for the time format of each specific language, and it is not conducive to the scalability of the program. Fortunately, Microsoft has provided us with the CultureInfo class, which inherits the IFormatProvider interface, so we can use it to get the desired time format (other formats can also be used to obtain, such as which currency ).

If you don't talk much about it, simply look at the Code:

Thread. currentThread. currentCulture = new System. globalization. cultureInfo ("de-de"); DateTime dt = Convert. toDateTime ("20:20:22"); Console. writeLine ("current language:" + Thread. currentThread. currentCulture. displayName); Console. writeLine ("Date: 20:20:22"); Console. writeLine ("d-short date mode:" + dt. toString ("d", Thread. currentThread. currentCulture); Console. writeLine ("D-long date mode:" + dt. toString ("D", Thread. currentThread. currentCulture); Console. writeLine ("f-full date/time mode (Short Time):" + dt. toString ("f", Thread. currentThread. currentCulture); Console. writeLine ("F-full date/time mode (Long Time):" + dt. toString ("F", Thread. currentThread. currentCulture); Console. writeLine ("g-regular date/time mode (Short Time):" + dt. toString ("g", Thread. currentThread. currentCulture); Console. writeLine ("G-regular date/time mode (Long Time):" + dt. toString ("G", Thread. currentThread. currentCulture); Console. writeLine ("m-month-day mode (m):" + dt. toString ("m", Thread. currentThread. currentCulture); Console. writeLine ("M-month-day mode (M):" + dt. toString ("M", Thread. currentThread. currentCulture); Console. writeLine ("o-round-trip date/time mode:" + dt. toString ("o", Thread. currentThread. currentCulture); Console. writeLine ("R-RFC1123 mode:" + dt. toString ("R", Thread. currentThread. currentCulture); Console. writeLine ("s-sortable date/time mode; ISO 8601:" + dt. toString ("s", Thread. currentThread. currentCulture); Console. writeLine ("t-short time mode:" + dt. toString ("t", Thread. currentThread. currentCulture); Console. writeLine ("T-long time mode:" + dt. toString ("T", Thread. currentThread. currentCulture); Console. writeLine ("u-common sortable date/time mode:" + dt. toString ("u", Thread. currentThread. currentCulture); Console. writeLine ("U-common sortable date/time mode:" + dt. toString ("U", Thread. currentThread. currentCulture); Console. writeLine ("Y-Year-month model:" + dt. toString ("Y", Thread. currentThread. currentCulture); Console. writeLine (dt. toString ("yyyyMMdd"); Console. readLine ();

  

In practical application, we can replace "de-de" in the first line with the language for user switching, and then set the current thread language as the current language type, then we can use the "Thread. currentThread. currentCulture. The previous running results are compared as follows:

  

(If the image display is incomplete, you can right-click the copy address and re-open a page to display the image separately)

As you can see from the results, if you want to obtain different language formats through CultureInfo, you cannot use custom formats such as "yyyyMMdd", "yyyy-MM-dd.

Appendix: the format of the obtained currency is decimal d = 123659.256 M; d. ToString ("c", Thread. CurrentThread. CurrentCulture. NumberFormat );

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.