C # detailed description of time format (datetime) to generate the required time format

Source: Internet
Author: User
Tags iso 8601 truncated

C # detailed usage of the datetime. tostring parameter format for time formatting. This article will introduce how to use the format.

Datetime. tostring (string, iformatprovider)

Detailed usage of the format parameter:

Format characters Association property/Description
D Shortdatepattern
D Longdatepattern
F Complete date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
G Regular (short date and short time)
G Regular (short date and long time)
M, m Monthdaypattern
R, R Rfc1123pattern
S Use sortabledatetimepattern of the local time (based on ISO 8601)
T Shorttimepattern
T Longtimepattern
U Universalsortabledatetimepattern is used to display the format of common time
U Use the complete date and time of the general time (long date and long time)
Y, y Yearmonthpattern

The following table lists the modes that can be merged to build a custom mode. These modes are case sensitive. For example, "mm" is recognized, but "mm" is not recognized ". If the custom mode contains white space characters or characters enclosed by single quotes, the output string page also contains these characters. Characters that are not defined as part of the format or are not defined as formatted characters are copied according to their original meaning.

Format mode Description
D A day in the month. A single-digit date has no leading zero.
Dd A day in the month. A single-digit date has a leading zero.
Ddd The abbreviation of a day in a week, which is defined in abbreviateddaynames.
Dddd The full name of the day of the week, which is defined in daynames.
M Number of months. One-digit month does not have a leading zero.
Mm Number of months. A single-digit month has a leading zero.
Mmm The abbreviated name of a month, which is defined in abbreviatedmonthnames.
Mmmm The complete name of the month, which is defined in monthnames.
Y Does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year that does not have the leading zero is displayed.
YY Does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year with the leading zero is displayed.
Yyyy This includes the four-digit year of the epoch.
Gg Period or epoch. This mode is ignored if the date to be formatted does not have an associated period or epoch string.
H The hour in 12-hour format. One-digit hours do not have a leading zero.
HH The hour in 12-hour format. The number of hours in a single digit has a leading zero.
H The hour in the 24-hour format. One-digit hours do not have a leading zero.
HH The hour in the 24-hour format. The number of hours in a single digit has a leading zero.
M Minutes, the number of minutes in a single digit does not start with zero.
Mm Minute, the number of minutes in a single digit has a leading zero.
S Second, the number of seconds in a single digit does not have the leading zero.
SS Second, the number of seconds in a single digit has a leading zero.
F Second decimal precision is one digit. The remaining numbers are truncated.
FF Second decimal precision is two. The remaining numbers are truncated.
Fff Second decimal precision is three. The remaining numbers are truncated.
FFFF The decimal precision of seconds is four digits. The remaining numbers are truncated.
Fffff The decimal precision of seconds is five digits. The remaining numbers are truncated.
Ffffff The decimal precision of seconds is six digits. The remaining numbers are truncated.
Fffffff The decimal precision of seconds is seven digits. The remaining numbers are truncated.
T The first character (if any) of the AM/PM indicator defined in amdesignator or pmdesignator ).
TT The AM/PM indicator defined in amdesignator or pmdesignator (if any ).
Z Time zone offset ("+" or "-" followed by only the hour ). One-digit hours do not have a leading zero. For example, the Standard Time in the Pacific is-8 ".
Zz Time zone offset ("+" or "-" followed by only the hour ). The number of hours in a single digit has a leading zero. For example, the Standard Time in the Pacific is-08 ".
Zzz Full Time Zone offset ("+" or "-" followed by hours and minutes ). The number of hours and minutes in a single digit has a leading zero. For example, the Pacific Standard Time is ".
: Default Time delimiter defined in timeseparator.
/ Default date delimiter defined in dateseparator.
% C C is the Format mode (if used separately ). If the Format mode is combined with the original and other formats, the "%" character can be omitted.
C C is any character. Display characters according to the original meaning. To display the backslash character, use "\".

Only the format modes listed in the second table can be used to create a custom mode. The standard format characters listed in the first table cannot be used to create a custom mode. The length of the custom mode must be at least two characters. For example,

Datetime. tostring ("D") returns the datetime value; "D" is the standard short date mode.

Datetime. tostring ("% d") returns the day of the month; "% d" is the custom mode.

Datetime. tostring ("D") returns the day of the month followed by a blank character. "D" is the custom mode.

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.