DateTime.ToString() Patterns

All the patterns:

0 MM/dd/yyyy 08/22/2006
1 dddd, dd MMMM yyyy Tuesday, 22 August 2006
2 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
3 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
4 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
5 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
6 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
7 MM/dd/yyyy HH:mm 08/22/2006 06:30
8 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
9 MM/dd/yyyy H:mm 08/22/2006 6:30
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
11 MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07
12 MMMM dd August 22
13 MMMM dd August 22
14 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
15 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
16 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
17 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
18 yyyy'-'MM'-'dd'T'HH':'mm':'ss 2006-08-22T06:30:07
19 HH:mm 06:30
20 hh:mm tt 06:30 AM
21 H:mm 6:30
22 h:mm tt 6:30 AM
23 HH:mm:ss 06:30:07
24 yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2006-08-22 06:30:07Z
25 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
26 yyyy MMMM 2006 August
27 yyyy MMMM 2006 August

The patterns for DateTime.ToString ( 'd' ) :

0 MM/dd/yyyy 08/22/2006

The patterns for DateTime.ToString ( 'D' ) :

0 dddd, dd MMMM yyyy Tuesday, 22 August 2006

The patterns for DateTime.ToString ( 'f' ) :

0 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
1 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
2 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
3 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM

The patterns for DateTime.ToString ( 'F' ) :

0 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( 'g' ) :

0 MM/dd/yyyy HH:mm 08/22/2006 06:30
1 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
2 MM/dd/yyyy H:mm 08/22/2006 6:30
3 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM

The patterns for DateTime.ToString ( 'G' ) :

0 MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07

The patterns for DateTime.ToString ( 'm' ) :

0 MMMM dd August 22

The patterns for DateTime.ToString ( 'r' ) :

0 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT

The patterns for DateTime.ToString ( 's' ) :

0 yyyy'-'MM'-'dd'T'HH':'mm':'ss 2006-08-22T06:30:07

The patterns for DateTime.ToString ( 'u' ) :

0 yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2006-08-22 06:30:07Z

The patterns for DateTime.ToString ( 'U' ) :

0 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( 'y' ) :

0 yyyy MMMM 2006 August

Building a custom DateTime.ToString Patterns

The following details the meaning of each pattern character. Note the K and z character.

d Represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero
dd Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero
ddd Represents the abbreviated name of the day of the week (Mon, Tues, Wed etc)
dddd Represents the full name of the day of the week (Monday, Tuesday etc)
h 12-hour clock hour (e.g. 7)
hh 12-hour clock, with a leading 0 (e.g. 07)
H 24-hour clock hour (e.g. 19)
HH 24-hour clock hour, with a leading 0 (e.g. 19)
m Minutes
mm Minutes with a leading zero
M Month number
MM Month number with leading zero
MMM Abbreviated Month Name (e.g. Dec)
MMMM Full month name (e.g. December)
s Seconds
ss Seconds with leading zero
t Abbreviated AM / PM (e.g. A or P)
tt AM / PM (e.g. AM or PM
y Year, no leading zero (e.g. 2001 would be 1)
yy Year, leadin zero (e.g. 2001 would be 01)
yyy Year, (e.g. 2001 would be 2001)
yyyy Year, (e.g. 2001 would be 2001)
K Represents the time zone information of a date and time value (e.g. +05:00)
z With DateTime values, represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. (e.g. +6)
zz As z but with leadin zero (e.g. +06)
zzz With DateTime values, represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. (e.g. +06:00)
f Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.
ff Represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value.
fff Represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.
ffff Represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value. While it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
fffff Represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value. While it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
ffffff Represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value. While it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
fffffff Represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value. While it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.
F Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero.
: Represents the time separator defined in the current DateTimeFormatInfo..::.TimeSeparator property. This separator is used to differentiate hours, minutes, and seconds.
/ Represents the date separator defined in the current DateTimeFormatInfo..::.DateSeparator property. This separator is used to differentiate years, months, and days.
" Represents a quoted string (quotation mark). Displays the literal value of any string between two quotation marks ("). Your application should precede each quotation mark with an escape character (\).
' Represents a quoted string (apostrophe). Displays the literal value of any string between two apostrophe (') characters.
%c Represents the result associated with a c custom format specifier, when the custom date and time format string consists solely of that custom format specifier. That is, to use the d, f, F, h, m, s, t, y, z, H, or M custom format specifier by itself, the application should specify %d, %f, %F, %h, %m, %s, %t, %y, %z, %H, or %M. For more information about using a single format specifier, see Using Single Custom Format Specifiers.

||\c || Represents the escape character, and displays the character "c" as a literal when that character is preceded by the escape character (\). To insert the backslash character itself in the result string, the application should use two escape characters ("\\").

Any other character copies any other character to the result string, without affecting formatting. ||

Author Paul Hayman

Paul is the COO of kwiboo ltd and has more than 20 years IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services.

Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

Rachit said:

Sometimes, you need to display milliseconds in the regular timestamp. Here's how it is: ToString("yyyy-MM-dd HH:mm:ss:fff")

18/Jun/2007 17:31 PM

John S said:

Here's a cheat sheet with all the .NET Format Strings:

http://john-sheehan.com/blog/index.php/net-cheat-sheets/

18/Jun/2007 21:43 PM

Mike said:

Can you create date string in format like this Tue 22nd Feb 2007 ?

18/Nov/2007 19:53 PM

phayman said:

.ToString("ddd dd MMM yyyy")

26/Nov/2007 11:09 AM

thank said:

awesome resource, thanks!

24/Jun/2008 19:59 PM

Stuey G said:

Hi, can you modify your example to show difference between the time formatters for Hour. HH & hh.

18/Jul/2008 09:58 AM

Saikat said:

Excellent.. Many thanks to you Paul

18/Sep/2008 10:51 AM

Alicia said:

Thank you!

23/Oct/2008 20:28 PM

RealityMasque said:

Does anyone know if there is a problem with the time zone? I've been testing formatting a DateTime using the 'k' character, but I never see anything, not even +0...

- O8

31/Oct/2008 12:26 PM

othman said:

RE: Does anyone know if there is a problem with the time zone? I've been testing formatting a DateTime using the 'k' character, but I never see anything, not even +0...

use "zz" or "zzz"

example:

"yyyy-MM-dd'T'HH:mm:ss.fffzzz" will show date as

2008-11-27T00:21:39.866+0200

27/Nov/2008 09:38 AM

Dev said:

Brilliant stuff, its handy to have all information on one single page. thanks buddy !

20/Mar/2009 09:53 AM

KD said:

I need to convert the date to format "YYYY-MM-DD HH:MM:SS"

EG : 2009-04-14 15:16:45

thnx in advance

05/May/2009 11:12 AM

WildAnjel said:

how to get 'nd', 'rd', or 'th' with date, like 20th

20/Jun/2009 23:20 PM

eldarion said:

Thanks for this great article! Much better then the Microsoft's official DateTime.ToString() documentation.

06/Aug/2009 23:32 PM

naresh said:

Awesome, thanks for this

21/Aug/2009 11:00 AM

.net Starter said:

Gr8 help Thanks for it !!

18/Sep/2009 10:31 AM

shc said:

very helpful about DateTime Tostring.http://www.cnblogs.com/shuchao/

23/Sep/2009 03:58 AM

Greatful Developer said:

This cheat sheet has been a life saver!!! Thank You

23/Oct/2009 18:25 PM

Adam said:

Umm...silly question maybe, but what programming language is this information for? A little basic introduction would be helpful. I came to it after googling for a custom-formated string representation of a javascript dater object, but I'm pretty sure it isn't standard javascript. Is "DateTIme" as custom/plugin javascript class or a standard class in some other language?

08/Jan/2010 12:32 PM

reubenh said:

excellent resource, tagging is a must!!

25/Jan/2010 22:29 PM

Kiran said:

Thanks a lot. It was useful to me.

04/Feb/2010 11:40 AM

lupita said:

tengo una duda enorme, me encargaron hacer un programa que me sirva para identificar todas las horas del mundo el detalle es que no se como hacerlo quisiera saber si me pudieras ayudar?

21/Feb/2010 00:33 AM

Avinash said:

Thanks a lot's

18/Mar/2010 03:46 AM

Adeel Fakhar said:

Awesome Article. I am very much impressed the way you deliver your knowledge to us. Awesome. Keep Sharing

18/Mar/2010 07:32 AM

Akhtar Jahan said:

Very helpfull, thanks

25/Mar/2010 05:43 AM

dr.mabuse said:

paul you`re a faca!

22/Apr/2010 15:26 PM

EB said:

Very helpful. Thank you.

05/Jun/2010 07:34 AM

Muthu said:

I have tried for milisec with below format but does not work.can you help me

yyyy-MM-dd HH:mm:ss:fff

MMMM dd, yyyy HH:mm:ss.fffzzz

I want the dataformat for June 17, 2010 13:03:03.4589 PM .

17/Jun/2010 08:01 AM

sonu said:

How to convert string date "Thu Mar 11 09:42:36 2010" in datetime format so that i can insert in a database

19/Jun/2010 09:16 AM

Lavanya said:

put so working examples up, when I try to use them they don't work.

08/Jul/2010 00:19 AM

Albert said:

how can I get the date in following format:

22nd of July, year 2010

11/Aug/2010 07:28 AM

andrei said:

thanks

12/Aug/2010 12:18 PM

Chris Schaller said:

If you're using .Net, then you can parse any custom date formats using DateTime.ParseExact then save it to the database the same way as any other DateTime object

DateTime.ParseExact("Thu Mar 11 09:42:36 2010", "ddd MMM dd HH:mm:ss yyyy", null)

I use this a lot when importing data from legacy or 3rd party apps when the date format is fixed but .Net or the database wont implicity convert it.

10/Sep/2010 00:33 AM

krish said:

hi...

  i need to covert 12-dec-2010 to 12-12-2010



 anyone can help me to find gud soln.....!
18/Oct/2010 12:33 PM

Palmani said:

Many more thanks for give like this. All format given...

31/Oct/2010 16:17 PM

Murat said:

Thanks Paul good job.

14/Dec/2010 08:53 AM

Alexandr said:

Big thanks!

02/Mar/2011 23:54 PM

Anil Singh said:

Thnak x

07/Apr/2011 13:25 PM

Hardik said:

Really ,,, very very good work... you saved my time

08/Apr/2011 11:47 AM

Hareesh said:

Thank u..

an excellent ariticle

11/May/2011 13:03 PM

justforfun said:

i am stuck ....

the problem is a string take a date from a txt file

then after that i cant convert this string to a datetime....

02/Aug/2011 01:41 AM

Dean said:

Thanks, I use this page at least once a week.

21/Sep/2011 16:27 PM

sharath said:

its good

28/Sep/2011 15:02 PM

sharath said:

its good

28/Sep/2011 15:02 PM

Morgs said:

This is the best trutorial ever! thanks to you...

13/Oct/2011 09:34 AM

Raji said:

Very useful notes... Thanks a lot!

Good job..

25/Oct/2011 04:41 AM

pichy said:

very gud....very nice information

15/Dec/2011 11:31 AM

arun said:

goog

07/Jan/2012 07:11 AM

Mat said:

Great Overview!! thxalot

23/Mar/2012 09:33 AM

sharik said:

thanks, great

18/Apr/2012 10:06 AM

Alfroy said:

Muchas gracias! Me ha ayudado mucho. El post es muy completo.

10/May/2012 16:22 PM

Sakshi Garg said:

Really that is a good way to understand trhanks a lot

26/Jun/2012 08:28 AM

Hemant said:

I am using yyyy-MM-ddTHH:mm:ss.fffffffzzz for dat of birth, but for DOB before 1980 is showing GMT+11 and for DOB after 1980 it is showing GMT+10 in string. How should i restrict it to GMT+10 for all DOB???

03/Jul/2012 09:33 AM

Manas said:

Good one -most helpful in developments

20/Sep/2012 08:53 AM

Su Thaw said:

I would like to change Sun Oct 14 13:33:40 MMT 2012 to String. I don't know what date time to string patterns should use for this date. Please kindly advice me. I am looking forward your advice.

14/Oct/2012 17:02 PM

Sovietik said:

I love ya men!!!!!!!!!!!!!!!!!!!!!!!!!

06/Nov/2012 13:26 PM

nonUSuser said:

please use PM time for your exemple. eg 18:30 so that HH:mm is really meaningfull

04/Mar/2013 14:38 PM

DevPer said:

I always look-up for your website to see your very useful post. thanks a lot.

26/Mar/2013 11:18 AM

Add Comment

Name
Comment
 

Your comment has been received and will be shown once it passes moderation.