Date Formatting in C#
Cheat sheet
<%= String.Format("{specifier}", DateTime.Now) %>
| Specifier | Description | Output |
|---|---|---|
| d | Short Date | 08/04/2007 |
| D | Long Date | 08 April 2007 |
| t | Short Time | 21:08 |
| T | Long Time | 21:08:59 |
| f | Full date and time | 08 April 2007 21:08 |
| F | Full date and time (long) | 08 April 2007 21:08:59 |
| g | Default date and time | 08/04/2007 21:08 |
| G | Default date and time (long) | 08/04/2007 21:08:59 |
| M | Day / Month | 08 April |
| r | RFC1123 date | Sun, 08 Apr 2007 21:08:59 GMT |
| s | Sortable date/time | 2007-04-08T21:08:59 |
| u | Universal time, local timezone | 2007-04-08 21:08:59Z |
| Y | Month / Year | April 2007 |
| dd | Day | 08 |
| ddd | Short Day Name | Sun |
| dddd | Full Day Name | Sunday |
| hh | 2 digit hour | 09 |
| HH | 2 digit hour (24 hour) | 21 |
| mm | 2 digit minute | 08 |
| MM | Month | 04 |
| MMM | Short Month name | Apr |
| MMMM | Month name | April |
| ss | seconds | 59 |
| tt | AM/PM | PM |
| yy | 2 digit year | 07 |
| yyyy | 4 digit year | 2007 |
| : | seperator, e.g. {0:hh:mm:ss} | 09:08:59 |
| / | seperator, e.g. {0:dd/MM/yyyy} | 08/04/2007 |
Currently rated 4.44 by 107 people
Rate Now!
Date Posted:
08 April 2007 21:11
Last Updated:
14 April 2007 22:37
Posted by:
Mikesdotnetting
Total Views to date:
110515



Comments
10 April 2009 11:19 from Navi
this is very usefull...
thanks
15 April 2009 10:02 from Pratul
Gr8 !! Gr8 !!, this is what exactly I was searching !
Thank you very much.
:)
24 April 2009 10:24 from bharath
nice information.tnx
04 June 2009 07:32 from stephen
Nice piece of info.. thanks a lot
22 July 2009 10:58 from Carsten
Perfect - exactly what i needed..
16 December 2009 10:27 from Krishnaraj
Nice piece of quick information. Thank you, saved my time
10 March 2010 12:04 from Ric
You might also want to mention that the formatting is available through the .toString() method as well as the string.Format one.
DateTime dt = new DateTime();
string formattedDate = dt.toString("dd/MM/yyyy");
03 June 2010 09:56 from da programmer
This Is The Shieeet
meghci boku
24 June 2010 06:25 from Jude Love
thanks dude
27 August 2010 06:17 from Anil
This is very useful.
23 September 2010 16:25 from Nilay
Thanks
I was looking for that in MSDN & it was too much information in it. This one is good.
07 June 2011 06:57 from dotJupiter
Great article. It summarizes all the parameters that I have been looking for a long time.
30 March 2012 17:09 from sabit
useful information about datetime format!!!
30 March 2012 17:11 from Sabit
useful information
15 May 2012 08:54 from Jezzrel
Yes, nice one. It's very useful...
01 June 2012 14:11 from nitesh
This is very useful.
01 July 2012 11:41 from Muktadir
Thanks
08 January 2013 04:12 from sam
good
16 January 2013 06:00 from Prasun Chatterjee
Great article.Thanks. But what if I need something like 2nd January,2013??
Thanks in advance.
17 January 2013 08:03 from Mikesdotnetting
@Prasun,
You will have to write your own method to generate the ordinal suffix based on the date, and then build your own string using that. There is no short cut formatter for what you want that I know of.