How to change DateTime format in C#

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

SortableDateTi­mePattern

yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)

u

UniversalSorta­bleDateTimePat­tern

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.CurrentCultureCultureInfo.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
az-Latn-AZ ---> 31.12.2013 18:07:09
be-BY ---> 31.12.13 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
ca-ES ---> 31/12/2013 18:07:09
cs-CZ ---> 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
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-DE ---> 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
dv-MV ---> 31/12/13 18:07:09
dv-MV ---> 31/12/13 18:07:09
el-GR ---> 31/12/2013 6:07:09 ??
el-GR ---> 31/12/2013 6:07:09 ??
en-US ---> 12/31/2013 6:07:09 PM
en-029 ---> 31/12/2013 18:07:09
en-AU ---> 31/12/2013 6:07:09 PM
en-BZ ---> 31/12/2013 06:07:09 PM
en-CA ---> 2013-12-31 6:07:09 PM
en-GB ---> 31/12/2013 18:07:09
en-IE ---> 31/12/2013 18:07:09
en-JM ---> 31/12/2013 18:07:09
en-NZ ---> 31/12/2013 6:07:09 p.m.
en-PH ---> 12/31/2013 6:07:09 PM
en-TT ---> 31/12/2013 06:07:09 PM
en-ZA ---> 2013-12-31 06:07:09 PM
en-ZW ---> 31/12/2013 6:07:09 PM
es-ES ---> 31/12/2013 18:07:09
es-AR ---> 31/12/2013 06:07:09 p.m.
es-BO ---> 31/12/2013 06:07:09 p.m.
es-CL ---> 31-12-2013 18:07:09
es-CO ---> 31/12/2013 6:07:09 p. m.
es-CR ---> 31/12/2013 06:07:09 p.m.
es-DO ---> 31/12/13 6:07:09 p. m.
es-EC ---> 31/12/2013 18:07:09
es-ES ---> 31/12/2013 18:07:09
es-GT ---> 31/12/2013 6:07:09 p. m.
es-HN ---> 31/12/2013 06:07:09 p.m.
es-MX ---> 31/12/2013 06:07:09 p. m.
es-NI ---> 31/12/2013 06:07:09 p.m.
es-PA ---> 31/12/13 6:07:09 p. m.
es-PE ---> 31/12/2013 06:07:09 p.m.
es-PR ---> 31/12/2013 06:07:09 p.m.
es-PY ---> 31/12/2013 06:07:09 p.m.
es-SV ---> 31/12/2013 06:07:09 p.m.
es-UY ---> 31/12/2013 18:07:09
es-VE ---> 31-12-2013 06:07:09 p.m.
et-EE ---> 31.12.2013 18:07:09
et-EE ---> 31.12.2013 18:07:09
eu-ES ---> 2013/12/31 18:07:09
eu-ES ---> 2013/12/31 18:07:09
fa-IR ---> 31/12/2013 06:07:09 ?.?
fa-IR ---> 31/12/2013 06:07:09 ?.?
fi-FI ---> 31.12.2013 18:07:09
fi-FI ---> 31.12.2013 18:07:09
fo-FO ---> 31-12-2013 18:07:09
fo-FO ---> 31-12-2013 18:07:09
fr-FR ---> 31/12/2013 18:07:09
fr-BE ---> 31-12-13 18:07:09
fr-CA ---> 2013-12-31 18:07:09
fr-FR ---> 31/12/2013 18:07:09
fr-CH ---> 31.12.2013 18:07:09
fr-LU ---> 31/12/2013 18:07:09
fr-MC ---> 31/12/2013 18:07:09
gl-ES ---> 31/12/2013 18:07:09
gl-ES ---> 31/12/2013 18:07:09
gu-IN ---> 31-12-13 18:07:09
gu-IN ---> 31-12-13 18:07:09
he-IL ---> 31/12/2013 18:07:09
he-IL ---> 31/12/2013 18:07:09
hi-IN ---> 31-12-2013 18:07:09
hi-IN ---> 31-12-2013 18:07:09
hr-HR ---> 31.12.2013. 18:07:09
hr-BA ---> 31.12.2013. 18:07:09
hr-HR ---> 31.12.2013. 18:07:09
hu-HU ---> 2013.12.31. 18:07:09
hu-HU ---> 2013.12.31. 18:07:09
hy-AM ---> 31.12.2013 18:07:09
hy-AM ---> 31.12.2013 18:07:09
id-ID ---> 31/12/2013 18:07:09
id-ID ---> 31/12/2013 18:07:09
is-IS ---> 31.12.2013 18:07:09
is-IS ---> 31.12.2013 18:07:09
it-IT ---> 31/12/2013 18.07.09
it-CH ---> 31.12.2013 18:07:09
it-IT ---> 31/12/2013 18.07.09
ja-JP ---> 2013/12/31 18:07:10
ja-JP ---> 2013/12/31 18:07:10
ka-GE ---> 31.12.2013 18:07:10
ka-GE ---> 31.12.2013 18:07:10
kk-KZ ---> 31-жел-13 18:07:10
kk-KZ ---> 31-жел-13 18:07:10
kn-IN ---> 31-12-13 18:07:10
kn-IN ---> 31-12-13 18:07:10
ko-KR ---> 2013-12-31 ?? 6:07:10
kok-IN ---> 31-12-2013 18:07:10
kok-IN ---> 31-12-2013 18:07:10
ko-KR ---> 2013-12-31 ?? 6:07:10
ky-KG ---> 31-дек 13 18:07:10
ky-KG ---> 31-дек 13 18:07:10
lt-LT ---> 2013-12-31 18:07:10
lt-LT ---> 2013-12-31 18:07:10
lv-LV ---> 31.12.2013. 18:07:10
lv-LV ---> 31.12.2013. 18:07:10
mi-NZ ---> 31/12/2013 6:07:10 p.m.
mk-MK ---> 31.12.2013 18:07:10
mk-MK ---> 31.12.2013 18:07:10
mn-MN ---> 2013-12-31 18:07:10
mr-IN ---> 31-12-2013 18:07:10
ms-MY ---> 31/12/2013 18:07:10
ms-BN ---> 31/12/2013 18:07:10
ms-MY ---> 31/12/2013 18:07:10
mt-MT ---> 31/12/2013 18:07:10
nb-NO ---> 31.12.2013 18:07:10
nl-NL ---> 31-12-2013 18:07:10
nl-BE ---> 31/12/2013 18:07:10
nl-NL ---> 31-12-2013 18:07:10
nn-NO ---> 31.12.2013 18:07:10
nb-NO ---> 31.12.2013 18:07:10
pa-IN ---> 31-12-13 ??? 06:07:10
pa-IN ---> 31-12-13 ??? 06:07:10
pl-PL ---> 2013-12-31 18:07:10
pl-PL ---> 2013-12-31 18:07:10
pt-BR ---> 31/12/2013 18:07:10
pt-BR ---> 31/12/2013 18:07:10
pt-PT ---> 31/12/2013 18:07:10
quz-BO ---> 31/12/2013 06:07:10 p.m.
quz-EC ---> 31/12/2013 18:07:10
quz-PE ---> 31/12/2013 06:07:10 p.m.
ro-RO ---> 31.12.2013 18:07:10
ru-RU ---> 31.12.2013 18:07:10
sa-IN ---> 31-12-2013 18:07:10
sa-IN ---> 31-12-2013 18:07:10
se-FI ---> 31.12.2013 18:07:10
se-NO ---> 31.12.2013 18:07:10
se-SE ---> 2013-12-31 18:07:10
sk-SK ---> 31.12.2013 18:07:10
sk-SK ---> 31.12.2013 18:07:10
sl-SI ---> 31.12.2013 18:07:10
sl-SI ---> 31.12.2013 18:07:10
sma-NO ---> 31.12.2013 18:07:10
sma-SE ---> 2013-12-31 18:07:10
smj-NO ---> 31.12.2013 18:07:10
smj-SE ---> 2013-12-31 18:07:10
smn-FI ---> 31.12.2013 18:07:10
sms-FI ---> 31.12.2013 18:07:10
sq-AL ---> 31.12.2013 18:07:10
sq-AL ---> 31.12.2013 18:07:10
sr-Latn-CS ---> 31.12.2013. 18:07:10
sr-Cyrl-BA ---> 31.12.2013. 18:07:10
sr-Cyrl-CS ---> 31.12.2013. 18:07:10
sr-Latn-BA ---> 31.12.2013. 18:07:10
sr-Latn-CS ---> 31.12.2013. 18:07:10
sv-SE ---> 2013-12-31 18:07:10
sv-FI ---> 31.12.2013 18:07:10
sv-SE ---> 2013-12-31 18:07:10
sw-KE ---> 12/31/2013 6:07:10 PM
sw-KE ---> 12/31/2013 6:07:10 PM
syr-SY ---> 31/12/2013 06:07:10 ?.?
syr-SY ---> 31/12/2013 06:07:10 ?.?
ta-IN ---> 31-12-2013 18:07:10
ta-IN ---> 31-12-2013 18:07:10
te-IN ---> 31-12-13 18:07:10
te-IN ---> 31-12-13 18:07:10
th-TH ---> 31/12/2556 18:07:10
th-TH ---> 31/12/2556 18:07:10
tn-ZA ---> 31/12/13 06:07:10 Mo Maitseboeng
tr-TR ---> 31.12.2013 18:07:10
tt-RU ---> 31.12.2013 18:07:10
tt-RU ---> 31.12.2013 18:07:10
uk-UA ---> 31.12.2013 18:07:10
uk-UA ---> 31.12.2013 18:07:10
ur-PK ---> 31/12/2013 6:07:10 PM
ur-PK ---> 31/12/2013 6:07:10 PM
uz-Latn-UZ ---> 31.12.2013 18:07:10
uz-Cyrl-UZ ---> 31.12.2013 18:07:10
uz-Latn-UZ ---> 31.12.2013 18:07:10
vi-VN ---> 31/12/2013 6:07:10 CH
vi-VN ---> 31/12/2013 6:07:10 CH
xh-ZA ---> 2013/12/31 06:07:10 Emva Kwemini
zh-CN ---> 2013/12/31 18:07:10
zh-HK ---> 31/12/2013 18:07:10
zh-MO ---> 31/12/2013 18:07:10
zh-SG ---> 31/12/2013 ?? 6:07:10
zh-TW ---> 2013/12/31 ?? 06:07:10
zu-ZA ---> 31-12-2013 06:07:10 Ekuseni


How to change DateTime format in C#
added 10 years 10 months ago

Contents related to 'How to change DateTime format in C#'

How to change number decimal seperator in C#?: This document explains how to change the number decimal seperator of the double and floating points in c# applications.

Converting String to Double in C#: Several example codes that are parsing string to double in C# language.

- How to POST and GET url with parameters in C#?
- Open a text file and read line by line in C#
- Convert.ToInt32() vs Int32.Parse() in C#
- How to C# Linq Group By by paramater/field names
- Simple Injector
- How to check memory consumption in c#
- How can I implement C/C++ "union" in C#?
- How to Open and Read From Excel File in C#
- Weak References
- Lazy Initialization
- Enable Nuget Package Restore
- OnixS FIX Engine
- Rapid Addition FIX API
- How to change DateTime format in C#
- How to change number decimal seperator in C#?
2
1