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

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

Recent Comments

Manoj Kulkarni 7/7/2015 12:53 PM
In response to Reading Excel Files Without Saving To Disk In ASP.NET
Nice article. Thank you...

osiris 7/6/2015 6:18 PM
In response to iTextSharp - Adding Text with Chunks, Phrases and Paragraphs
Great tutorial, thank you guys...

Fred Solbakken 7/6/2015 1:30 PM
In response to The Difference Between @Helpers and @Functions In WebMatrix
Is it possible to call e.g. @Functions.IsBeforeToday("2010/3/22") directly from a jQuery AJAX like...

ben 7/5/2015 11:23 AM
In response to Web Pages - Efficient Paging Without The WebGrid
as usual, your page of resources saves the day yet again... is there a donate button? im happy to u...

Preson Reddy 7/5/2015 2:40 AM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Model Binding And Forms
hi i just saw ur post on the order form its really helping me with my campus project i was just how...

Nacho 7/3/2015 2:32 PM
In response to Handling JSON Arrays returned from ASP.NET Web Services with jQuery
Thank you, works great and very well explained....

Gayan 7/3/2015 6:20 AM
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
Great Article thanks...

Semil 7/1/2015 7:03 AM
In response to iTextSharp - Drawing shapes and Graphics
I have created a rectangle using above methode. Now I want to add a text in the center of this How I...

Satyabrata Mohapatra 6/30/2015 6:12 PM
In response to Reading Excel Files Without Saving To Disk In ASP.NET
Ahh.....this is awesome. Happy to see after a long time you wrote a article on web form :D...

Marty 6/30/2015 7:16 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Mike, you're the Man! Another great article. So incredibly helpful. I'm definitely going to buy your...