How to change DateTime format in C#
Changing the serialization format of a DateTime object and parsing a string to a DateTime object can be a challenging task when you are developing a multi-lingual large-scale application. For example, 3rd party libraries might use different culture settings. Your database connection might need a specific datetime format. Users or clients might prefer different datetime formats.
For these reasons, you should be careful about handling DateTime formatting issues, while you are developing a multi-lingual application in .Net environment (e.g. C# language). Displayed datetime formats, processed datetime formats, and stored datetime formats can be different.
There are several methods to manage DateTime format in C# programming language.
Changing DateTime format with String.Format
Here is the example code:
DateTime dt = DateTime.Now;
String.Format("{0:y yy yyy yyyy}", dt); // 20 20 2020 2020
Changing DateTime format using ToString() method
Another style of changing the format is:
dt.ToString("MMMM dd, yyyy"); // February 28, 2020
Change .Net DateTime to Microsoft SQL (MSSQL) Date/Time format
dt.ToString("yyyy-MM-dd HH:mm:ss"); // 2020-02-28 16:13:47
The list of available datetime format specifiers are given below:
|
Specifier |
DateTimeFormatInfo property |
Pattern value (for en-US culture) |
|
t |
ShortTimePattern |
h:mm tt |
|
d |
ShortDatePattern |
M/d/yyyy |
|
T |
LongTimePattern |
h:mm:ss tt |
|
D |
LongDatePattern |
dddd, MMMM dd, yyyy |
|
f |
(combination of D and t) |
dddd, MMMM dd, yyyy h:mm tt |
|
F |
FullDateTimePattern |
dddd, MMMM dd, yyyy h:mm:ss tt |
|
g |
(combination of d and t) |
M/d/yyyy h:mm tt |
|
G |
(combination of d and T) |
M/d/yyyy h:mm:ss tt |
|
m, M |
MonthDayPattern |
MMMM dd |
|
y, Y |
YearMonthPattern |
MMMM, yyyy |
|
r, R |
RFC1123Pattern |
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*) |
|
s |
SortableDateTimePattern |
yyyy'-'MM'-'dd'T'HH':'mm':'ss (*) |
|
u |
UniversalSortableDateTimePattern |
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*) |
|
|
|
(*) = culture independent |
Editing Current Culture to manage DateTime format
To change the DateTime format in the application-wide, you need to edit the current culture of the application. Here is an example of this:
String culture = "af-ZA";
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Console.WriteLine(culture + " ---> " + DateTime.Now);
You may also change the DateTime format for the GUI thread:
Thread.CurrentThread.CurrentUICulture = ...
Available DateTime formats for the given culture strings are listed below:
af-ZA ---> 2013/12/31 06:07:09 NM.
af-ZA ---> 2013/12/31 06:07:09 NM.
ar-SA ---> 28/02/35 06:07:09 ?
ar-AE ---> 31/12/2013 06:07:09 ?
ar-BH ---> 31/12/2013 06:07:09 ?
ar-DZ ---> 31-12-2013 18:07:09
ar-EG ---> 31/12/2013 06:07:09 ?
ar-IQ ---> 31/12/2013 06:07:09 ?
ar-JO ---> 31/12/2013 06:07:09 ?
ar-KW ---> 31/12/2013 06:07:09 ?
ar-LB ---> 31/12/2013 06:07:09 ?
ar-LY ---> 31/12/2013 06:07:09 ?
ar-MA ---> 31-12-2013 18:07:09
ar-OM ---> 31/12/2013 06:07:09 ?
ar-QA ---> 31/12/2013 06:07:09 ?
ar-SA ---> 28/02/35 06:07:09 ?
ar-SY ---> 31/12/2013 06:07:09 ?
ar-TN ---> 31-12-2013 18:07:09
ar-YE ---> 31/12/2013 06:07:09 ?
az-Latn-AZ ---> 31.12.2013 18:07:09
az-Cyrl-AZ ---> 31.12.2013 18:07:09
be-BY ---> 31.12.13 18:07:09
bg-BG ---> 31.12.2013 г. 18:07:09
bs-Latn-BA ---> 31.12.2013 18:07:09
ca-ES ---> 31/12/2013 18:07:09
cs-CZ ---> 31. 12. 2013 18:07:09
cy-GB ---> 31/12/13 18:07:09
da-DK ---> 31-12-2013 18:07:09
de-DE ---> 31.12.2013 18:07:09
de-AT ---> 31.12.2013 18:07:09
de-CH ---> 31.12.2013 18:07:09
de-LI ---> 31.12.2013 18:07:09
de-LU ---> 31.12.2013 18:07:09
en-US ---> 12/31/2013 6:07:09 PM
en-GB ---> 31/12/2013 18:07:09
en-AU ---> 31/12/2013 6:07:09 PM
en-CA ---> 2013-12-31 6:07:09 PM
en-ZA ---> 2013-12-31 06:07:09 PM
en-NZ ---> 31/12/2013 6:07:09 p.m.
en-PH ---> 12/31/2013 6:07:09 PM
es-ES ---> 31/12/2013 18:07:09
es-MX ---> 31/12/2013 06:07:09 p. m.
es-AR ---> 31/12/2013 06:07:09 p.m.
fr-FR ---> 31/12/2013 18:07:09
fr-CA ---> 2013-12-31 18:07:09
fr-BE ---> 31-12-13 18:07:09
it-IT ---> 31/12/2013 18.07.09
it-CH ---> 31.12.2013 18:07:09
ja-JP ---> 2013/12/31 18:07:10
ko-KR ---> 2013-12-31 ?? 6:07:10
zh-CN ---> 2013/12/31 18:07:10
zh-TW ---> 2013/12/31 ?? 06:07:10
ru-RU ---> 31.12.2013 18:07:10
tr-TR ---> 31.12.2013 18:07:10
ro-RO ---> 31.12.2013 18:07:10
pl-PL ---> 2013-12-31 18:07:10