Different Strings Format for Date and Time
The Short Date ("d") Format Specifier
The "d" standard format specifier is a date and time format string that is defined by a current culture's DateTimeFormatInfo.ShortDatePattern property.
DateTime date1 = new DateTime(2011, 10, 15);
// Displays date independent of local culture settings(MM/dd/YYYY)
Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo));
// Displays 10/15/2011
// Displays date in french culture format
Console.WriteLine(date1.ToString("d", CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays 15/10/2011
// Displays date in swedish date format
Console.WriteLine(date1.ToString("d", CultureInfo.CreateSpecificCulture("sv-SE")));
// Displays 2011-10-15
The Long Date ("D") Format Specifier <javascript:void(0)>
The "D" standard format specifier is a date and time format string that is defined by the current culture's DateTimeFormatInfo.LongDatePattern property.
date = new DateTime(2011, 10, 15);
// Displays Date in denmark culture format
Console.WriteLine(date.ToString("D",CultureInfo.CreateSpecificCulture("de-DE")));
// output: samstag, 15. oktober 2011
// Displays Date in belarus culture format
Console.WriteLine(date.ToString("D",CultureInfo.CreateSpecificCulture("nl-BE")));
// output: zaterdag 15 oktober 2011
The Short Time ("t") Format Specifier
The "t" format specifier represents short time ("t") format string. It will display the time (hours and minutes only) in the specified culture.
date = new DateTime(2011, 10, 15,10,30,15);
// Displays time in finland culture format
Console.WriteLine(date.ToString("t", CultureInfo.CreateSpecificCulture("sv-FI")));
// output: 10:30
// Displays time in mexico spanish culture format
Console.WriteLine(date.ToString("t", CultureInfo.CreateSpecificCulture("es-MX")));
// output: 10:30 A.m
The Long Time ("T") Format Specifier
The "T" format specifier represents long time format string. It will display the time (hours, minutes and seconds) in the specified culture.
// Displays full time in finland culture format
Console.WriteLine(date.ToString("T", CultureInfo.CreateSpecificCulture("sv-FI")));
// output: 10:30:15
// Displays full time in mexico spanish culture format
Console.WriteLine(date.ToString("T", CultureInfo.CreateSpecificCulture("es-MX")));
// output: 10:30:15 A.m
The Full Date Short Time ("f") Format Specifier
The "f" format specifier represents a combination of the long date ("D") and short time ("t") patterns, separated by a space or a culturally specified separator.
Console.WriteLine(date.ToString("f",CultureInfo.CreateSpecificCulture("en-US")));
// saturday, october 15, 2011 10:30 AM
Console.WriteLine(date.ToString("f",CultureInfo.CreateSpecificCulture("pt-BR")));
//sabado, 15 de outubro de 2011 10:30
The Full Date Full Time ("F") Format Specifier
The "f" format specifier represents a combination of the long date ("D") and Full time ("T") patterns, separated by a space or a culturally specified separator.
Console.WriteLine(date.ToString("F", CultureInfo.CreateSpecificCulture("en-US")));
// saturday, october 15, 2011 10:30:15 AM
Console.WriteLine(date.ToString("F", CultureInfo.CreateSpecificCulture("pt-BR")));
//sabado, 15 de outubro de 2011 10:30:15
The General Date Short Time ("g") Format Specifier <javascript:void(0)>
The "g" standard format specifier represents a combination of the short date ("d") and short time ("t") patterns, separated by a space or a culturally specified separator.
date = new DateTime(2011, 10, 15, 10, 30, 15);
// Displays Short Date and Short Time in brazil portuguese culture format
Console.WriteLine(date.ToString("g", DateTimeFormatInfo.InvariantInfo));
// output: 10/15/2011 10:30
// Displays Short Date and Short Time in brazil portuguese culture format
Console.WriteLine(date.ToString("g", CultureInfo.CreateSpecificCulture("es-MX")));
// output: 15/10/2011 10:30 a.m
The General Date Long Time ("G") Format Specifier <javascript:void(0)>
The "G" standard format specifier represents a combination of the short date ("d") and long time ("T") patterns, separated by a space or a culturally specified separator.
// Displays Short Date and Long Time in US Format
Console.WriteLine(date.ToString("G",CultureInfo.CreateSpecificCulture("en-us")));
// output: 10/15/2011 10:30:15 AM
// Displays Short Date and Long Time in Belarus format
Console.WriteLine(date.ToString("G",CultureInfo.CreateSpecificCulture("nl-BE")));
// output: 15/10/2011 10:30:15
The Month ("M", "m") Format Specifier <javascript:void(0)>
The "M" or "m" standard format specifier represents a custom date and time format string defined by the current DateTimeFormatInfo.MonthDayPattern property of a specified culture.
// Displays month with date
Console.WriteLine(date.ToString("m",CultureInfo.CreateSpecificCulture("en-us")));
// output: october 15
// Displays month in numeric format
Console.WriteLine(date.ToString("MM", CultureInfo.CreateSpecificCulture("ms-MY")));
// output: 10
// Displays only month
Console.WriteLine(date.ToString("MMMM",CultureInfo.CreateSpecificCulture("ms-MY")));
// output: oktober
The Year Month ("Y", "y") Format Specifier <javascript:void(0)>
The "Y" or "y" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.YearMonthPattern property of a specified culture.
Console.WriteLine(date.ToString("Y", CultureInfo.CreateSpecificCulture("en-US")));
// October, 2011
Console.WriteLine(date.ToString("y", CultureInfo.CreateSpecificCulture("pt-BR")));
// outubro de 2011
The Round-trip ("O", "o") Format Specifier <javascript:void(0)>
The "O" or "o" standard format specifier represents a custom date and time format string using a pattern that preserves time zone information
When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture. Therefore, result is always the same regardless of the culture used or the format provider supplied. Strings that are passed to the
Parse or
ParseExact method must conform exactly to this custom format pattern, or a
FormatException is thrown.
DateTime dOld, dNew;
string sDate;
// Round-trip a local time.
dOld = DateTime.SpecifyKind(new DateTime(2011, 10, 15, 10, 30, 15), DateTimeKind.Local);
sDate = dOld.ToString("o");
dNew = DateTime.Parse(sDate, null, DateTimeStyles.RoundtripKind);
Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", dOld, dOld.Kind, dNew, dNew.Kind);
// output: Round-tripped 10/15/2011 10:30:15 AM Local to 10/15/2011 10:30:15 AM Local.
// Round-trip time in an unspecified time zone.
Old = DateTime.SpecifyKind(new DateTime(2011, 10, 15, 10, 30, 15), DateTimeKind.Unspecified);
sDate = dOld.ToString("o");
dNew = DateTime.Parse(sDate, null, DateTimeStyles.RoundtripKind);
Console.WriteLine("Round-tripped {0} {1} to {2} {3}.", dOld, dOld.Kind, dNew, dNew.Kind);
// output: Round-tripped 10/15/2011 10:30:15 AM Unspecified to 10/15/2011 10:30:15 AMM Unspecified.
// Round-trip a DateTimeOffset value.
DateTimeOffset originalDTO = new DateTimeOffset(2011, 10, 15, 10, 30, 15, new TimeSpan(0, -5, 0));
sDate = originalDTO.ToString("o");
DateTimeOffset newDTO = DateTimeOffset.Parse(sDate, null, DateTimeStyles.RoundtripKind);
Console.WriteLine("Round-tripped {0} to {1}.", originalDTO, newDTO);
// output: Round-tripped 10/15/2011 10:30:15 AM -00:05 to 10/15/2011 10:30:15 AM -00:05
The RFC1123 ("R", "r") Format Specifier <javascript:void(0)>
The "R" or "r" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.RFC1123Pattern read-only property. When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture. Therefore, result is always the same regardless of the culture used or the format
Although the RFC 1123 standard expresses a time as Coordinated Universal Time (UTC), the formatting operation does not modify the value of the DateTime or DateTimeOffset object that is being formatted. Therefore, the application must convert the date and time value to UTC before it performs the formatting operation. To perform this conversion, DateTime values can call the DateTime.ToUniversalTime method, and DateTimeOffset values can call the ToUniversalTime method.
DateTimeOffset dateOffset = new DateTimeOffset(date, TimeZoneInfo.Local.GetUtcOffset(date));
Console.WriteLine(date.ToUniversalTime().ToString("r"));
// output: Fri, 14 Oct 2011 21:00:00 GMT
Console.WriteLine(dateOffset.ToUniversalTime().ToString("r"));
// output: Fri, 14 Oct 2011 21:00:00 GMT
The Sortable ("s") Format Specifier <javascript:void(0)>
The "s" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.SortableDateTimePattern read-only property. When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture. Therefore, result is always the same regardless of the culture used or the format
Console.WriteLine(date.ToString("s"));
// output: 2011-10-15T00:00:00
The Universal Sortable ("u") Format Specifier <javascript:void(0)>
The "u" standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.UniversalSortableDateTimePattern property. The pattern reflects 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 custom format string is "yyyy'-'MM'-'dd HH':'mm':'ss'Z'". When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.
Although the result string should express a time as Coordinated Universal Time (UTC), no conversion of the original DateTime or DateTimeOffset value is performed during the formatting operation. Therefore, the application must convert the date and time value to UTC before formatting it. To perform this conversion, DateTime values can call the DateTime.ToUniversalTime method, and DateTimeOffset values can call the ToUniversalTime method
// Show date in universal time format
Console.WriteLine(date.ToUniversalTime().ToString("u"));
// output: 2011-15-10 07:30:15Z
The Universal Full ("U") Format Specifier <javascript:void(0)>
The "U" standard format specifier represents a custom date and time format string that is defined by a specified culture's DateTimeFormatInfo.FullDateTimePattern property. The pattern is the same as the "F" pattern. However, the DateTime value is automatically converted to UTC before it is formatted.
// Show date in universal full time format independent of culture
Console.WriteLine(date.ToString("U",CultureInfo.InvariantCulture));
// output: Saturday, 15 October, 2011 07:30:15 AM
// Show date in South Africa culture universal full time format
Console.WriteLine(date.ToString("U",CultureInfo.CreateSpecificCulture("af-ZA")));
// output: 15 Oktober 2011 07:30:15 AM
here
Share this to your friends. One of your friend is
waiting for your share.