Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Date and Time Format Strings

Posted by Viral Sarvaiya on November 21, 2009

Date and time format strings are used to specify which formatting to apply when a datetime data type is represented as a string.

Date and time format strings fall into either standard or custom format strings. Standard datetime format strings are those that the framework has predefined for commonly used datetime patterns, while custom datetime format strings are supplied in cases where the standard format specifiers do not provide the type of formatting required.

Standard DateTime Format Strings

Standard datetime format strings are used to return string equivalents of datetime types in predefined patterns. When used with ToString, a standard datetime format string parameter takes the form

object.ToString ( "F" )

where F is a single alphabetic character, which can be any of the built-in format characters specified below.

The following table describes the standard datetime format strings. Note that the patterns produced by these format specifiers are influenced by the settings in the Regional Options control panel. Computers using different cultures or different currency settings will display different patterns.

Format specifier Name Description
d Short date pattern Displays a pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread or by a specified format provider.
D Long date pattern Displays a pattern defined by the DateTimeFormatInfo.LongDatePattern property associated with the current thread or by a specified format provider.
t Short time pattern Displays a pattern defined by the DateTimeFormatInfo.ShortTimePattern property associated with the current thread or by a specified format provider.
T Long time pattern Displays a pattern defined by the DateTimeFormatInfo.LongTimePattern property associated with the current thread or by a specified format provider.
f Full date/time pattern ( short time ) Displays a combination of the long date and short time patterns, separated by a space.
F Full date/time pattern ( long time ) Displays a pattern defined by the DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by a specified format provider.
g General date/time pattern ( short time ) Displays a combination of the short date and short time patterns, separated by a space.
G General date/time pattern ( long time ) Displays a combination of the short date and long time patterns, separated by a space.
M or m Month day pattern Displays a pattern defined by the DateTimeFormatInfo.MonthDayPattern property associated with the current thread or by a specified format provider.
R or r RFC1123 pattern Displays a pattern defined by the DateTimeFormatInfo.RFC1123Pattern property associated with the current thread or by a specified format provider. This is a defined standard and the property is read-only; therefore, it is always the same regardless of the culture used, or the format provider supplied. The property references the CultureInfo.InvariantCulture property and follows the custom pattern ‘ddd, dd MMMM yyyy HH:mm:ss G\MT’. Note that the ‘M’ in ‘GMT’ needs an escape character so it is not interpreted.
s Sortable date/time pattern; conforms to ISO 8601 Displays a pattern defined by the DateTimeFormatInfo.SortableDateTimePattern property associated with the current thread or by a specified format provider. The property references the CultureInfo.InvariantCultureproperty, and the format follows the custom pattern ‘yyyy-MM-ddTHH:mm:ss’.
u Universal sortable date/time pattern Displays a pattern defined by the DateTimeFormatInfo. UniversalSortableDateTimePattern property associated with the current thread or by a specified format provider. Because it is a defined standard and the property is read-only, the pattern is always the same regardless of culture or format provider. The format follows the custom pattern ‘yyyy-MM-dd HH:mm:ssZ’.
U Universal sortable date/time pattern Displays a pattern defined by the DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by a specified format provider. Note that the time displayed is for the Universal, rather than local time.
Y or y Year month pattern Displays a pattern defined by the DateTimeFormatInfo.YearMonthPattern property associated with the current thread or by a specified format provider.
Any other single character Unknown specifier

The date and time separators displayed by format strings are defined by the DateSeparator and TimeSeparator characters associated with the DateTimeFormat property of the current culture. However, in cases where the InvariantCulture is referenced by the r, s, and u specifiers, the characters associated with the DateSeparator and TimeSeparator characters do not change based on the current culture.

All datetime format strings that consist of a single alphabetic character ( without a leading or trailing white space ) are interpreted as standard datetime format strings.

If a string is interpreted as a standard datetime format string and contains one of the standard datetime format specifiers, then the datetime value is formatted accordingly. However, if a string is interpreted as a standard format string but does not contain one of the standard format specifiers, then a FormatException is thrown.

Any datetime format string that does not fit the definition of a standard datetime format string is interpreted as a custom datetime format string.

For More Reference

http://authors.aspalliance.com/aspxtreme/aspnet/types/datetimeformatstrings.aspx

http://www.123aspx.com/Rotor/RotorSrc.aspx?rot=40515

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: