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: 203468

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

kawish 4/24/2015 7:04 AM
In response to Using The Themes Helper In Razor Web Pages
Nice:)...

Mahdi 4/23/2015 9:49 PM
In response to Adding a New Row In The Razor WebGrid
Thank u very very. its useful for me...

Jerrie Pelser 4/23/2015 4:24 PM
In response to Entity Framework 6 Recipe: Storing And Managing Time
Nice article Mike! I think there is an error in the House property getter of MovieFormModel. Your...

mike 4/22/2015 4:45 PM
In response to Parameter Queries in ASP.NET with MS Access
If the insert statement got failed how w can i get the error message of as string?...

frank 4/22/2015 9:44 AM
In response to Sessions in ASP.NET 5
Mike can you write up an article showcasing the difference / upgrade paths of ASP.net Web Pages and...

Scott 4/21/2015 11:39 PM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
Excellent! Just what I need for the current application I am working on. Even to the extent that I a...

Benjamin 4/21/2015 4:57 AM
In response to Creating a Connection String and Working with SQL Server LocalDB
Hi Mike, great article. I am learning a lot. As commented @Evita, the &lt;Key> Data Annotation is I...

Wayne Hudson 4/20/2015 9:14 PM
In response to Inline Editing With The WebGrid
Thank you very much for your articles. They've helped me a lot. How would you handle inline such...

Dan 4/20/2015 5:01 PM
In response to Sessions in ASP.NET 5
Can I ask how these sort of options are affected by the IIS configuration options when hosted in be...

Shaheen 4/20/2015 3:36 PM
In response to Conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
Perfect! Saved me from wasting hours....