DateTime format string

Standard DateTime Format Strings

See: http://msdn.microsoft.com/zh-cn/library/az4se3k1(VS.80).aspx

Format specifier name Explanation

d

Short date pattern

Represented by the current ShortDatePattern custom-defined properties DateTime format string.

For example, for fixed regional custom format string "MM / dd / yyyy".

D

Long date mode

Represented by the current LongDatePattern custom attributes defined DateTime format string.

For example, for fixed regional custom format string "dddd, dd MMMM yyyy".

f

Complete date / time pattern (short time)

Length represents the date (D) and a combination of short-time (t) mode, separated by a space.

F

Complete date / time pattern (long)

Represented by the current FullDateTimePattern custom-defined properties DateTime format string.

For example, for fixed regional custom format string "dddd, dd MMMM yyyy HH: mm: ss".

g

Conventional date / time mode (short)

It represents a combination of short date (d) and short (t) mode, separated by a space.

G

General date / time pattern (long)

Short date represents (d) and a combination of time (T) mode, separated by a space.

M or m

May Day mode

Represented by the current MonthDayPattern custom attributes defined DateTime format string.

For example, for fixed regional custom format string "MMMM dd".

O

Round-trip date / time pattern

Custom mode indication information zone reserved DateTime format string. This mode is dedicated to serving DateTime formats (including text form Kind property). Then Parse or ParseExact the correct Kind use with attribute values may be reversed analysis format string.

Custom format string is "yyyy '-' MM '-' dd'T'HH ':' mm ':' ss.fffffffK".

This mode is used specifier is defined standard. So what regional or format provided by the provider, whether the use is that it is always the same.

R or r

RFC1123 mode

Represented by the current RFC1123Pattern custom-defined properties DateTime format string. This mode is defined by the standard, and the attribute is read-only. So what regional or format provided by the provider, whether the use is that it is always the same.

Is defined as the format string "ddd, dd MMM yyyy HH ':' mm ':' ss 'GMT'".

Formatting does not modify being formatted DateTime value of the object. Thus, the application must use this value before conversion format specifier to Coordinated Universal Time (UTC).

s

Sortable date / time mode; meet ISO 8601

Represented by the current SortableDateTimePattern custom-defined properties DateTime format string. This mode is defined by the standard, and the attribute is read-only. So what regional or format provided by the provider, whether the use is that it is always the same.

Custom format string is "yyyy '-' MM '-' dd'T'HH ':' mm ':' ss".

t

Short time Mode

Represented by the current ShortTimePattern custom-defined properties DateTime format string.

For example, for fixed regional custom format string "HH: mm".

T

Long time mode

Represented by the current LongTimePattern custom attributes defined DateTime format string.

For example, for fixed regional custom format string "HH: mm: ss".

in

General sortable date / time pattern

Represented by the current UniversalSortableDateTimePattern custom attributes defined DateTime format string. This mode is defined by the standard, and the attribute is read-only. So what regional or format provided by the provider, whether the use is that it is always the same.

Custom format string is "yyyy '-' MM '-' dd HH ':' mm ':' ss'Z '".

Formatting zone conversion is not performed when the date and time. Therefore, the application must convert the local date and time before using this format specifier to Coordinated Universal Time (UTC).

The

General sortable date / time pattern

表示由当前 FullDateTimePattern 属性定义的自定义 DateTime 格式字符串。

此模式与完整日期/长时间 (F) 模式相同。但是,格式化将作用于等效于正在格式化的 DateTime 对象的协调世界时 (UTC)。

Y 或 y

年月模式

表示由当前 YearMonthPattern 属性定义的自定义 DateTime 格式字符串。

例如,用于固定区域性的自定义格式字符串为“yyyy MMMM”。

任何其他单个字符

(未知说明符)

未知说明符将引发运行时格式异常。

 

 

 

自定义 DateTime 格式字符串

详见:http://msdn.microsoft.com/zh-cn/library/8kb3ddd4(VS.80).aspx

格式说明符 说明

d

将月中日期表示为从 1 至 31 的数字。一位数字的日期设置为不带前导零的格式。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

dd

将月中日期表示为从 01 至 31 的数字。一位数字的日期设置为带前导零的格式。

ddd

将一周中某天的缩写名称表示为当前 System.Globalization.DateTimeFormatInfo.AbbreviatedDayNames 属性中定义的名称。

dddd(另加任意数量的“d”说明符)

将一周中某天的全名表示为当前 System.Globalization.DateTimeFormatInfo.DayNames 属性中定义的名称。

f

表示秒部分的最高有效位。

请注意,如果“f”格式说明符单独使用,没有其他格式说明符,则该说明符被看作是“f”标准 DateTime 格式说明符(完整日期/时间模式)。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

将此格式说明符与 ParseExactTryParseExact 方法一起使用时,所用“f”格式说明符的数目指示要分析的秒部分的最高有效位位数。

ff

表示秒部分的两个最高有效位。

fff

表示秒部分的三个最高有效位。

ffff

表示秒部分的四个最高有效位。

fffff

表示秒部分的五个最高有效位。

ffffff

表示秒部分的六个最高有效位。

fffffff

表示秒部分的七个最高有效位。

F

表示秒部分的最高有效位。如果该位为零,则不显示任何信息。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

将此格式说明符与 ParseExactTryParseExact 方法一起使用时,所用“F”格式说明符的数目指示要分析的秒部分的最高有效位最大位数。

FF

表示秒部分的两个最高有效位。但不显示尾随零(或两个零位)。

FFF

表示秒部分的三个最高有效位。但不显示尾随零(或三个零位)。

FFFF

表示秒部分的四个最高有效位。但不显示尾随零(或四个零位)。

FFFFF

表示秒部分的五个最高有效位。但不显示尾随零(或五个零位)。

FFFFFF

表示秒部分的六个最高有效位。但不显示尾随零(或六个零位)。

FFFFFFF

表示秒部分的七个最高有效位。但不显示尾随零(或七个零位)。

g 或 gg(另加任意数量的“g”说明符)

表示时期或纪元(例如 A.D.)。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该说明符。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

h

将小时表示为从 1 至 12 的数字,即通过 12 小时制表示小时,自午夜或中午开始对整小时计数。因此,午夜后经过的某特定小时数与中午过后的相同小时数无法加以区分。小时数不进行舍入,一位数字的小时数设置为不带前导零的格式。例如,给定时间为 5:43,则此格式说明符显示“5”。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

hh, hh(另加任意数量的“h”说明符)

将小时表示为从 01 至 12 的数字,即通过 12 小时制表示小时,自午夜或中午开始对整小时计数。因此,午夜后经过的某特定小时数与中午过后的相同小时数无法加以区分。小时数不进行舍入,一位数字的小时数设置为带前导零的格式。例如,给定时间为 5:43,则此格式说明符显示“05”。

H

将小时表示为从 0 至 23 的数字,即通过从零开始的 24 小时制表示小时,自午夜开始对小时计数。一位数字的小时数设置为不带前导零的格式。

HH, HH(另加任意数量的“H”说明符)

将小时表示为从 00 至 23 的数字,即通过从零开始的 24 小时制表示小时,自午夜开始对小时计数。一位数字的小时数设置为带前导零的格式。

K

表示 DateTime.Kind 属性的不同值,即“Local”、“Utc”或“Unspecified”。此说明符以文本形式循环设置 Kind 值并保留时区。如果 Kind 值为“Local”,则此说明符等效于“zzz”说明符,用于显示本地时间偏移量,例如“-07:00”。对于“Utc”类型值,该说明符显示字符“Z”以表示 UTC 日期。对于“Unspecified”类型值,该说明符等效于“”(无任何内容)。

m

将分钟表示为从 0 至 59 的数字。分钟表示自前一小时后经过的整分钟数。一位数字的分钟数设置为不带前导零的格式。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

mm, mm(另加任意数量的“m”说明符)

将分钟表示为从 00 至 59 的数字。分钟表示自前一小时后经过的整分钟数。一位数字的分钟数设置为带前导零的格式。

M

将月份表示为从 1 至 12 的数字。一位数字的月份设置为不带前导零的格式。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

MM

将月份表示为从 01 至 12 的数字。一位数字的月份设置为带前导零的格式。

MMM

将月份的缩写名称表示为当前 System.Globalization.DateTimeFormatInfo.AbbreviatedMonthNames 属性中定义的名称。

MMMM

将月份的全名表示为当前 System.Globalization.DateTimeFormatInfo.MonthNames 属性中定义的名称。

s

将秒表示为从 0 至 59 的数字。秒表示自前一分钟后经过的整秒数。一位数字的秒数设置为不带前导零的格式。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

ss, ss(另加任意数量的“s”说明符)

将秒表示为从 00 至 59 的数字。秒表示自前一分钟后经过的整秒数。一位数字的秒数设置为带前导零的格式。

t

表示当前 System.Globalization.DateTimeFormatInfo.AMDesignatorSystem.Globalization.DateTimeFormatInfo.PMDesignator 属性中定义的 A.M./P.M. 指示符的第一个字符。如果正在格式化的时间中的小时数小于 12,则使用 A.M. 指示符;否则使用 P.M. 指示符。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

tt, tt(另加任意数量的“t”说明符)

将 A.M./P.M. 指示符表示为当前 System.Globalization.DateTimeFormatInfo.AMDesignatorSystem.Globalization.DateTimeFormatInfo.PMDesignator 属性中定义的内容。如果正在格式化的时间中的小时数小于 12,则使用 A.M. 指示符;否则使用 P.M. 指示符。

y

将年份表示为最多两位数字。如果年份多于两位数,则结果中仅显示两位低位数。如果年份少于两位数,则该数字设置为不带前导零的格式。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

yy

将年份表示为两位数字。如果年份多于两位数,则结果中仅显示两位低位数。如果年份少于两位数,则用前导零填充该数字使之达到两位数。

yyy

将年份表示为三位数字。如果年份多于三位数,则结果中仅显示三位低位数。如果年份少于三位数,则用前导零填充该数字使之达到三位数。

请注意,对于年份可以为五位数的泰国佛历,此格式说明符将显示全部五位数。

yyyy

将年份表示为四位数字。如果年份多于四位数,则结果中仅显示四位低位数。如果年份少于四位数,则用前导零填充该数字使之达到四位数。

请注意,对于年份可以为五位数的泰国佛历,此格式说明符将呈现全部五位数。

yyyyy(另加任意数量的“y”说明符)

将年份表示为五位数字。如果年份多于五位数,则结果中仅显示五位低位数。如果年份少于五位数,则用前导零填充该数字使之达到五位数。

如果存在额外的“y”说明符,则用所需个数的前导零填充该数字使之达到“y”说明符的数目。

z

表示系统时间距格林威治时间 (GMT) 以小时为单位测量的带符号时区偏移量。例如,位于太平洋标准时区中的计算机的偏移量为“-8”。

偏移量始终显示为带有前导符号。加号 (+) 指示小时数早于 GMT,减号 (-) 指示小时数迟于 GMT。偏移量范围为 –12 至 +13。一位数字的偏移量设置为不带前导零的格式。偏移量受夏时制影响。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

zz

表示系统时间距格林威治时间 (GMT) 以小时为单位测量的带符号时区偏移量。例如,位于太平洋标准时区中的计算机的偏移量为“-08”。

偏移量始终显示为带有前导符号。加号 (+) 指示小时数早于 GMT,减号 (-) 指示小时数迟于 GMT。偏移量范围为 –12 至 +13。一位数字的偏移量设置为带前导零的格式。偏移量受夏时制影响。

zzz, zzz(另加任意数量的“z”说明符)

表示系统时间距格林威治时间 (GMT) 以小时和分钟为单位测量的带符号时区偏移量。例如,位于太平洋标准时区中的计算机的偏移量为“-08:00”。

偏移量始终显示为带有前导符号。加号 (+) 指示小时数早于 GMT,减号 (-) 指示小时数迟于 GMT。偏移量范围为 –12 至 +13。一位数字的偏移量设置为带前导零的格式。偏移量受夏时制影响。

:

当前 System.Globalization.DateTimeFormatInfo.TimeSeparator 属性中定义的时间分隔符,用于区分小时、分钟和秒。

/

当前 System.Globalization.DateTimeFormatInfo.DateSeparator 属性中定义的日期分隔符,用于区分年、月和日。

"

带引号的字符串(引号)。显示两个引号 (") 之间的任意字符串的文本值。在每个引号前使用转义符 (\)。

'

带引号的字符串(撇号)。显示两个撇号 (') 字符之间的任意字符串的文本值。

%c

当自定义 DateTime 格式字符串只包含自定义格式说明符“c”时,表示与该自定义格式说明符关联的结果。也就是说,若要单独使用自定义格式说明符“d”、“f”、“F”、“h”、“m”、“s”、“t”、“y”、“z”、“H”或“M”,请指定“%d”、“%f”、“%F”、“%h”、“%m”、“%s”、“%t”、“%y”、“%z”、“%H”或“%M”。有关使用单个格式说明符的更多信息,请参见使用单个自定义格式说明符

\c

转义符。当字符“c”前带有转义符 (\) 时,将该字符显示为文本。若要将反斜杠字符本身插入结果字符串,请使用两个转义符(“\\”)。

任何其他字符

所有其他字符被复制到结果字符串中,而且不影响格式化。

 

 

转载于:https://www.cnblogs.com/jenneyblog/archive/2012/10/24/datetime_format.html

Guess you like

Origin blog.csdn.net/weixin_34226706/article/details/93370507