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

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

- Andreas

Helpful article!

Is there any option to display the date format as days since 01/01/1900

e.g. 21/08/2015 as 42237

Thanks in advance.

Recent Comments

sandeep 8/28/2015 7:03 AM
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
very good article, i like it........keep writing such quality article in future. thx Mike....

Hassan, MVC Learner 8/28/2015 6:37 AM
In response to Get The Drop On ASP.NET MVC DropDownLists
Great Help, simple, great and patiently explained article !...

Anvesh 8/28/2015 12:39 AM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
what if we are taking postback values from FormCollections instead of an array int[] category. How...

Ben 8/27/2015 10:50 PM
In response to Simple File Download Protection with ASP.NET
Is it possible to setup your project to publish files to outside of your root directory? I would to...

Fred 8/26/2015 12:50 AM
In response to WebMatrix Opens Wrong Version Of Visual Studio
I enjoyed many of your tutorials but the problem is none of the tutorials are combined like most be....

Muhammad Ashikuzzaman 8/25/2015 2:48 PM
In response to Managing Checkboxes And Radios In ASP.NET Razor Web Pages
That's a very good tips for razor...

Sergey 8/25/2015 8:32 AM
In response to More Flexible Routing For ASP.NET Web Pages
Hi. How I can set up my site to get urldata from link for default page? site.com/default/1-...

Tony Gray 8/25/2015 6:27 AM
In response to Adding Validation
Hi Mike, Really helpful article and series. Thanks. Small typo in 4th paragraph you have so...

amanda n 8/25/2015 12:38 AM
In response to Solving the Operation Must Use An Updateable Query error
Thank you very much. I'm a uni student and while solving coding problems is usually enjoyable and me...

salman 8/23/2015 9:25 AM
In response to How To Send Email In ASP.NET MVC
thanks ......