Date Formatting in C#

Cheat sheet detailing formatting options for dates and times in C#.

Example Usage

<%= String.Format("{specifier}", DateTime.Now) %>
@DateTime.Now.ToString("F")
@DateTime.Now.ToString("hh:mm:ss.fff")

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
fff milliseconds 120
FFF milliseconds without trailing zero 12
tt AM/PM PM
yy 2 digit year 07
yyyy 4 digit year 2007
: Hours, minutes, seconds separator, e.g. {0:hh:mm:ss} 09:08:59
/ Year, month , day separator, e.g. {0:dd/MM/yyyy} 08/04/2007
. milliseconds separator  

Date Posted:
Last Updated:
Posted by:
Total Views to date: 199166

32 Comments

- Navi

this is very usefull...
thanks

- Pratul

Gr8 !! Gr8 !!, this is what exactly I was searching !
Thank you very much.
:)

- bharath

nice information.tnx

- stephen

Nice piece of info.. thanks a lot

- Carsten

Perfect - exactly what i needed..

- Krishnaraj

Nice piece of quick information. Thank you, saved my time

- 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");

- da programmer

This Is The Shieeet

meghci boku

- Jude Love

thanks dude

- Anil

This is very useful.

- Nilay

Thanks

I was looking for that in MSDN & it was too much information in it. This one is good.

- dotJupiter

Great article. It summarizes all the parameters that I have been looking for a long time.

- sabit

useful information about datetime format!!!

- Sabit

useful information

- Jezzrel

Yes, nice one. It's very useful...

- nitesh

This is very useful.

- Muktadir

Thanks

- sam

good

- Prasun Chatterjee

Great article.Thanks. But what if I need something like 2nd January,2013??
Thanks in advance.

- Mike

@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.

- Navina

tnks 4 ur info..
quite interesting....!

- Shreyas

Good one...Thanks..

- Mayur

Thanks..........................

- sandeep sharma

Nice

Thanks !!

- vikas

nice

- Bob Rodes

The short date option (d) doesn't include leading zeros in month and day, so it would actually render 8/4/2007. To get the leading zeros the format needs to be spelled out: MM/dd/yyyy or dd/MM/yyyy. http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx has all the formats you never use...

- Mike

@Bob

The "d" option (short date) is culture specific. Leading zeros are omitted if your culture is en-US. They are retained for en-GB.

- Jomon John

It's good.
Thank you....

- suhas

thanx.....sir.!

- Abolfazl RoshanZamir

very informative... thanks for sharing sir...

- akil

i need date format "mmm yyyy". Ex : Mar 2015, what is the specifier ??

- Mike

@akil,

You need capital Ms: "MMM yyyy"

DateTime.Now.ToString("MMM yyyy")
Add your comment

If you have any comments to make about this article, please use this form to do so. Make sure that your comment relates specifically to the article above. More general comments can be posted through the form on the Contact page.

Please note, all comments are moderated, and I end up deleting quite a lot. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Requests to fix your code (post a question to forums.asp.net instead, please)
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam

I do not pass email addresses on to spammers, so a valid one will assist me in responding to you personally if required.

Recent Comments

Lorenzo 3/26/2015 8:21 AM
In response to iTextSharp - Introducing Tables
Hi Mike How can I add padding to all cells in the table? Kind Regards Lorenzo...

Satyabrata Mohapatra 3/25/2015 8:11 AM
In response to How To Send Email In ASP.NET MVC
Great article. Simple and up to the point....

Afzaal Ahmad Zeeshan 3/24/2015 8:17 PM
In response to How To Send Email In ASP.NET MVC
A great way to teach the MVC framework for sending the emails... Also, what I found helpful was the...

Jim H 3/24/2015 2:32 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Model Binding And Forms
Thank you. This helps....

wazz 3/22/2015 5:48 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
great info!!...

rael 3/21/2015 8:53 PM
In response to Getting the identity of the most recently added record
I spent hours trying to figure how to achieve this in C#. This article helped me. Thanks a lot...

Stephen 3/21/2015 8:48 PM
In response to Ajax with Classic ASP using jQuery
This was very helpful, thanks:)...

patrick voes 3/19/2015 10:19 AM
In response to iTextSharp - Introducing Tables
Thank you! very helpfull....

Bigmachini 3/19/2015 6:13 AM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
This just made my day, afternoon, evening, night... was looking for a way of doing this without to a...

Bobbyg 3/19/2015 4:14 AM
In response to HTML Helpers For Forms In Razor Web Pages
Nice article. There are bugs in MVC 5 with dropdowns retaining values from other screens and them. I...