I always forget the formatting method of time.

Source: Internet
Author: User
{0: yy-mm-dd hh: mm}

By using customDatetimeFormat description to create your own customDatetimeFormat String, you can better control the formatting . Combine one or more custom format specifiers to constructDatetimeFormat mode. In fact, most standardsDatetimeThe format specifiers are currently applicable. The alias of the formatting mode specified in.

The following table describes the custom format specifiers and their results. The output of these format specifiers is affected by the current culture and settings in the "region options" control panel.

Format description Description
D Displays the current date of a month, represented by a number between 1 and 31, including 1 and 31. If the date has only one digit (1-9), it is displayed as a digit.

Note that if the "D" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard short date format specifier. If the "D" format specifier is passed together with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.

Dd Displays the current date of a month, represented by a number between 1 and 31, including 1 and 31. If the date has only one digit (1-9), format it with a leading 0 (01-09 ).
Ddd Display the specifiedDatetime. If no specific valid format is providedProgram(Implement ), Use Of Attribute and the current region associated with the currently used thread. OtherwiseAbbreviateddaynamesAttribute.
Dddd (plus any number of additional "D" characters) Display the specifiedDatetimeThe full name of the date. If no specific valid format provider is provided (a non-empty object can implementIformatprovider), UseDatetimeformatOf Attribute and the current region associated with the currently used thread. OtherwiseDaynamesAttribute.
F Displays the seconds in one digit.

Note that if the "f" format specifier is used separately and there are no other custom format strings, it is interpreted as a complete (long date + short time) format specifier. If the "f" format specifier is passed together with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.

FF Displays the seconds in two digits.
Fff Displays the seconds in three digits.
FFFF Displays the seconds in four digits.
Fffff Displays the seconds in five digits.
Ffffff Displays the seconds in six digits.
Fffffff Displays the seconds in seven digits.
G or Gg (plus any number of additional "G" characters) Display the specifiedDatetime(For example, A. D .). If no specific valid format provider is provided (a non-empty object can implementIformatprovider), ThenDatetimeformatThe associated calendar and the current culture associated with the current thread are determined.

Note that if the "G" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard regular format specifier. If the "G" format specifier is passed together with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.

H Displays a specified number in the range of 1 to 12.DatetimeThe number of hours, which indicates the total number of hours since midnight (displayed as 12) or noon (also displayed as 12. If this format is used separately, it cannot be different from the time before or after noon for an hour. If the hour is a single number (1-9), it is displayed as a single number. It indicates that no round occurs in the hour. For example,DatetimeIf it is, 5 is returned.
HH, HH (plus any number of additional "H" characters) Displays a specified number in the range of 1 to 12.DatetimeThe number of hours, which indicates the total number of hours since midnight (displayed as 12) or noon (also displayed as 12. If this format is used separately, it cannot be different from the time before or after noon for an hour. If the hour is a single number (1-9), format it with 0 (01-09) in front ).
H Displays a specified number in the range of 0 to 23.DatetimeThe number of hours, which indicates the total number of hours since midnight (displayed as 0. If the hour is a single number (0-9), it is displayed as a single number.
HH, HH (plus any number of additional "H" characters) Displays a specified number in the range of 0 to 23.DatetimeThe number of hours, which indicates the total number of hours since midnight (displayed as 0. If the hour is a single number (0-9), format it with 0 (01-09) in front ).
M Displays a specified number in the range of 0 to 59.DatetimeThe number of minutes, which indicates the number of minutes that have elapsed since the last hour. If the minute is a digit (0-9), it is displayed as a digit.

Note that if the "M" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard month-day format specifier. If the "M" format specifier is passed together with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.

Mm, mm (plus any number of additional "M" characters) Displays a specified number in the range of 0 to 59.DatetimeThe number of minutes, which indicates the number of minutes that have elapsed since the last hour. If the minute is a digit (0-9), format it with a leading 0 (01-09 ).
M Shows the month, represented by a number between 1 and 12 (including 1 and 12. If the month is a digit (1-9), it is displayed as a digit.

Note that if the "M" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard month-day format specifier. If the "M" format specifier is passed together with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.

Mm Shows the month, represented by a number between 1 and 12 (including 1 and 12. If the month is a digit (1-9), format it with a leading 0 (01-09 ).
Mmm Display the specifiedDatetimeThe abbreviation of the month section. If no specific valid format provider is provided (a non-empty object can implementIformatprovider), UseDatetimeformatOf Attribute and the current region associated with the current thread. OtherwiseAbbreviatedmonthnamesAttribute.
Mmmm Display the specifiedDatetimeThe full name of the month. If no specific valid format provider is provided (a non-empty object can implementIformatprovider), UseDatetimeformatOf Attribute and the current region associated with the current thread. OtherwiseMonthnamesAttribute.
S Displays a specified number in the range of 0 to 59.DatetimeThe number of seconds, which indicates the whole number of seconds since the last minute. If the second is a digit (0-9), it is displayed as only one digit.

Note that if the "S" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard sortable date/time mode format specifier. If the "S" format specifier is passed together with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.

SS, SS (plus any number of additional "S" characters) Displays a specified number in the range of 0 to 59.DatetimeThe number of seconds, which indicates the whole number of seconds since the last minute. If the second is a digit (0-9), format it with a leading 0 (01-09 ).
T display the first character of the. m./P. M. Indicator of the specified datetime . If no specific valid format provider is provided (a non-empty object can implement iformatprovider with the expected attributes ), the datetimeformat (or ) attributes and the current culture associated with the current thread. Otherwise, use the amdesignator (or pmdesignator ) Attribute from the specified iformatprovider . If the total number of hours for the specified datetime is less than 12, amdesignator is used. Otherwise, use pmdesignator .

note that if the "T" format specifier is used separately, there are no other custom format strings, it is interpreted as a standard long-time format specifier. If the "T" format specifier is passed together with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.

TT, TT (plus any number of additional "T" characters) Display the specifiedDatetimeA. M./P. M. Indicator. If no specific valid format provider is provided (a non-empty object can implementIformatprovider), UseDatetimeformatOfAmdesignator(OrPmdesignator) Attribute and the current region associated with the current thread. Otherwise, useIformatproviderOfAmdesignator(OrPmdesignator. IfDatetimeUseAmdesignator. Otherwise, usePmdesignator.
Y A maximum of two digits can be used to display the specifiedDatetime. Ignore the first two digits of the year. If the year is a digit (1-9), it is displayed as a digit.

Note that if the "y" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard short date format specifier. If the "y" format specifier is passed together with other custom format specifiers or "%" characters, it is interpreted as a custom format specifier.

YY A maximum of two digits can be used to display the specifiedDatetime. Ignore the first two digits of the year. If the year is a digit (1-9), format it with a leading 0 (01-09 ).
Yyyy Display the specifiedDatetime(Including epochs ). If the length of a year is less than four digits, add zero to the beginning to increase the length of the year to four digits.
Z The Time Zone offset of the current time zone of the system is displayed in units of the hour. The total offset is displayed with a leading symbol (0 is displayed as "+ 0"), indicating the number of hours earlier than Greenwich Mean Time (+) or later than Greenwich Mean Time. The value range is-12 to + 13. If the offset is a single digit (0-9), it is displayed as a single digit with a proper leading symbol. The time zone is set in the form of + X or-X, where X is the hourly deviation from GMT. The displayed deviation is affected by the daylight saving time.
Zz The Time Zone offset of the current time zone of the system is displayed in units of the hour. The total offset is displayed with a leading or trailing sign (0 is displayed as "+ 00"), indicating the hours earlier than Greenwich Mean Time (+) or later than Greenwich Mean Time. The value range is-12 to + 13. If the offset is a single digit (0-9), format it to the front with 0 (01-09) and a proper leading symbol. The time zone is set in the form of + X or-X, where X is the hourly deviation from GMT. The displayed deviation is affected by the daylight saving time.
Zzz, ZZZ (plus any number of additional "Z" characters) The Time Zone offset of the current time zone of the system is displayed in units of hours and minutes. The offset is always displayed with a leading or trailing sign (0 is displayed as "+ 00:00"), indicating the hours earlier than Greenwich Mean Time (+) or later than Greenwich Mean Time. The value range is-to +. If the offset is a single digit (0-9), format it to the front with a leading 0 (01-09) with an appropriate leading symbol. The time zone is set in the form of + X or-X, where X is the hourly deviation from GMT. The displayed deviation is affected by the daylight saving time.
: Time separator.
/ Date separator.
" A string with quotation marks. Displays the text value of any string between two quotation marks after the escape character.
' A string with quotation marks. Displays the text value of any string between two "'" characters.
%C WhereCBoth standard and custom format specifiers display the custom format mode associated with the format specifiers.

Note that if the format specifier is used separately as a single character, it will be interpreted as a standard format specifier. Only format specifiers that contain two or more characters are interpreted as custom format specifiers. The specifiers can be defined as both standard and custom format specifiers. to display the custom format of such specifiers, add the "%" symbol before the specifiers.

\C WhereCIs any character. The Escape Character shows the next character as text. In this context, the escape character cannot be used to create an escape sequence (for example, "\ n" indicates a line break ).
Any other character Other characters are directly written to the output string as text.

DirectionDatetime. tostringWhen the custom mode is passed, the mode must be at least two characters long. If only "D" is passed, the common language runtime interprets it as a standard format specifier because all single format specifiers are interpreted as standard format specifiers. If a single "H" is passed, an exception is thrown because there is no standard "H" format specifier. To format only a single custom format, add a space before or after the description. For example, the format string "H" is interpreted as a custom format string.

The following example illustrates howDatetimeCreate a custom formatted string. This example assumes that the current region is American English (En-US ).

[Visual Basic]Dim mydate as new datetime (2000, 1, 1, 0, 0, 0) dim mystring as string = mydate. tostring ("dddd-D-mmmm") 'in the U. s. english culture, mystring has the value: '"Saturday-1-January ". mystring = mydate. tostring ("yyyy gg") 'in the U. s. english cultures, mystring has the value: "2000. d. ".

 
[C #]Datetime mydate = new datetime (2000, 1, 1, 0, 0, 0); string mystring = mydate. tostring ("dddd-D-mmmm"); // in the U. s. english culture, mystring has the value: // "Saturday-1-January ". mystring = mydate. tostring ("yyyy gg"); // in the U. s. english cultures, mystring has the value: "2000. d. ".

See

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.