Datetime usage 2

Source: Internet
Author: User

Any project will inevitably encounter the datetime display problem ,. although the. NET Framework provides rich and diverse display methods, I rarely use it. Because I forget the details, I have to worry about whether the month is before or before the year. The date separator is "/". or "\", or.

 

Therefore, every time the date is displayed, my writing is usually similar to the following code, so that you can choose based on your needs, control the display format and order as you like, and do not need to tangle with what the separator is, in the tostring method, each character represents the first letter of the corresponding word and is not hard to remember.

Datetime dt = new datetime (int32 12, 6,13, 19,0 );
DT. tostring ("yyyy-mm-dd hh: mm: ss tt zz"); // 2012-12-06 01:19:00 + 08
    • Y: Year
    • M: Month
    • D: Day
    • H or H: hour
    • M: minute
    • S: seconds
    • T: 12-hour upper afternoon (AM/pm)
    • Z: Time Zone

These characters are case sensitive. "H" and "H" correspond to 12-hour and 24-hour respectively. Because the first letter of month-month and minute-minute is "M", it is case sensitive. The month is in upper case and the minute is in lower case.

Diversified display of day and month

Sometimes the display of a month may be varied, including displaying the beginning in English, displaying only the first three letters, and separating them with commas. In this case, I have seen some code that uses switch-case to determine the month in sequence, which is a little troublesome. In fact, the above method can be completed with a slight change.

For example, the number of "M" in the tostring method determines the display format.

DT. tostring ("mm"); // 12
DT. tostring ("mmm"); // Dec
DT. tostring ("mmmm"); // December

Similarly, the "D" representing the day also has the same feature, the difference is that the "DDD" and "dddd" table day rather than day.

DT. tostring ("DD"); // 06
DT. tostring ("DDD"); // thu
DT. tostring ("dddd"); // Date of Thursday Culture in different regions

Time display will inevitably encounter regional cultural problems. For example, in the initial example of running, "PM" may be displayed as "PM ":

DT. tostring ("yyyy-mm-dd hh: mm: ss tt zz"); // 2012-12-06 01:19:00 PM + 08

When the month is displayed in text, "December" may be displayed directly ":

DT. tostring ("mm"); // 12
DT. tostring ("mmm"); // January 1, December
DT. tostring ("mmmm"); // January 1, December

Different regional cultures often cause problems. To avoid problems, you must specify a specific regional culture. In general, it is commonly used in English, and the method of specifying is also the easiest. Just use the static attribute invariantculture of the cultureinfo class:

DT. tostring ("yyyy-mm-dd hh: mm: ss tt zz", cultureinfo. invariantculture); // 01:19:00 + 08

To use other regional cultures, you need to instantiate the specified cultureinfo class. For example, the following example shows the week of Chinese, Japanese, and English:

DT. tostring ("dddd"); // Thursday
DT. tostring ("dddd", new cultureinfo ("Ja-JP"); // wooden day
DT. tostring ("dddd", cultureinfo. invariantculture); // Thursday

Here, "Ja-JP" is used to indicate Japanese, and "Ja" is also available ". You can specify "ZH-tw" to display traditional Chinese characters. If you want to display text of other regional cultures, you can find the corresponding characters in the list on this msdn page.

Datetime usage 2

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.