C # time format (Datetime) Usage details _ Practical skills

Source: Internet
Author: User

DateTime.ToString (String, IFormatProvider)

parameter Format format detail usage :

Td>u
format character Association property/Description
d shortdatepattern
D longdatepattern
F full date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
g General (short date and short time)
G General (short date and long time)
m, M monthdaypattern
R, R rfc1123pattern
s Sortabledatetimepattern with local time (based on ISO 8601)
t shorttimepattern
t longtimepattern
universalsortabledatetimepattern The format used to display Universal Time
u full date and time with Universal Time (long date and long time) Inter)
y, y yearmonthpattern

The following table lists the patterns that can be merged to construct custom schemas. These patterns are case-sensitive; for example, "MM" is recognized, but "MM" is not recognized. If the custom pattern contains white space characters or characters enclosed in single quotes, the output string page will also contain those characters. Characters that are not defined as part of a format pattern or that are not defined as format characters are copied verbatim.

Format pattern Description
D One day of the month. The date of one number does not have a leading zero.
Dd One day of the month. The date of a single number has a leading zero.
Ddd The abbreviated name of the day of the week, defined in Abbreviateddaynames.
dddd The full name of the day of the week, defined in DayNames.
M The number of months. A single number of months does not have a leading zero.
Mm The number of months. A one-digit month has a leading zero.
MMM The abbreviated name of the month, defined in AbbreviatedMonthNames.
MMMM The full name of the month, defined in MonthNames.
Y The year that does not contain eras. If the year that does not contain the era is less than 10, the year is displayed with no leading zeros.
Yy The year that does not contain eras. If the year that does not contain the era is less than 10, the year with leading zeros is displayed.
yyyy A four-digit year that includes the era.
Gg Period or era. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
H 12-hour hours. The number of hours in a single digit does not have a leading zero.
hh 12-hour hours. The number of hours in a single digit has a leading zero.
H 24-hour hours. The number of hours in a single digit does not have a leading zero.
HH 24-hour hours. The number of hours in a single digit has a leading zero.
M Minutes, one number of minutes does not have a leading zero.
Mm Minutes, the number of minutes in one digit has a leading zero.
S Seconds, the number of seconds in a number does not have a leading zero.
Ss Seconds, a number of seconds has a leading zero.
F The decimal precision of the second is one digit. The remaining digits are truncated.
Ff The decimal precision of the second is two digits. The remaining digits are truncated.
Fff The decimal precision of the second is three digits. The remaining digits are truncated.
Ffff The decimal precision of the second is four digits. The remaining digits are truncated.
Fffff The decimal precision of the second is five digits. The remaining digits are truncated.
Ffffff The decimal precision of the second is six digits. The remaining digits are truncated.
Fffffff The decimal precision of the second is seven digits. The remaining digits are truncated.
T The am/pm defined in AMDesignator or PMDesignator indicates the first character of the item, if one exists.
Tt A am/pm indicator, if present, that is defined in AMDesignator or PMDesignator.
Z Time zone offset ("+" or "-" followed only by hours). The number of hours in a single digit does not have a leading zero. For example, the Pacific Standard Time is "-8".
Zz Time zone offset ("+" or "-" followed only by hours). The number of hours in a single digit has a leading zero. For example, the Pacific Standard Time is "-08".
zzz Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes for a single digit has a leading zero. For example, the Pacific Standard Time is " -08:00".
: The default time separator that is defined in TimeSeparator.
/ The default date separator that is defined in DateSeparator.
%c where c is the format pattern (if used alone). If the format pattern is merged with a literal character or another format pattern, you can omit the "%" character.
C where c is any character. Display characters according to the literal meaning. To display the backslash character, use "\".

Only the format patterns listed in the second table above can be used to create custom patterns; The standard format characters listed in the first table cannot be used to create custom patterns. The length of the custom pattern is at least two characters;

DateTime.ToString ("D") returns the DateTime value, and "D" is the standard short date pattern.

DateTime.ToString ("%d") returns the day of the month, and "%d" is a custom pattern.

DateTime.ToString ("D") returns the day of the month followed by a blank character, and "D" is a custom pattern.

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.