Date Formatting in C#
<%= 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.43 by 35 people
Rate Now!
Date Posted:
08 April 2007 21:11
Last Updated:
14 April 2007 22:37
Posted by:
Mikesdotnetting
Total Views to date:
24386
Printer Friendly Version
Comments
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.



10 April 2009 11:19 from Navi
this is very usefull...
thanks