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: Sunday, April 8, 2007 9:11 PM
Last Updated: Wednesday, October 23, 2013 1:43 PM
Posted by: Mikesdotnetting
Total Views to date: 181651

27 Comments

Friday, April 10, 2009 11:19 AM - Navi

this is very usefull...
thanks

Wednesday, April 15, 2009 10:02 AM - Pratul

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

Friday, April 24, 2009 10:24 AM - bharath

nice information.tnx

Thursday, June 4, 2009 7:32 AM - stephen

Nice piece of info.. thanks a lot

Wednesday, July 22, 2009 10:58 AM - Carsten

Perfect - exactly what i needed..

Wednesday, December 16, 2009 10:27 AM - Krishnaraj

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

Wednesday, March 10, 2010 12:04 PM - 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");

Thursday, June 3, 2010 9:56 AM - da programmer

This Is The Shieeet

meghci boku

Thursday, June 24, 2010 6:25 AM - Jude Love

thanks dude

Friday, August 27, 2010 6:17 AM - Anil

This is very useful.

Thursday, September 23, 2010 4:25 PM - Nilay

Thanks

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

Tuesday, June 7, 2011 6:57 AM - dotJupiter

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

Friday, March 30, 2012 5:09 PM - sabit

useful information about datetime format!!!

Friday, March 30, 2012 5:11 PM - Sabit

useful information

Tuesday, May 15, 2012 8:54 AM - Jezzrel

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

Friday, June 1, 2012 2:11 PM - nitesh

This is very useful.

Sunday, July 1, 2012 11:41 AM - Muktadir

Thanks

Tuesday, January 8, 2013 4:12 AM - sam

good

Wednesday, January 16, 2013 6:00 AM - Prasun Chatterjee

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

Thursday, January 17, 2013 8:03 AM - 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.

Wednesday, July 10, 2013 3:03 PM - Navina

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

Wednesday, August 14, 2013 10:52 AM - Shreyas

Good one...Thanks..

Wednesday, October 30, 2013 11:34 AM - Mayur

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

Thursday, April 24, 2014 12:28 PM - sandeep sharma

Nice

Thanks !!

Saturday, May 31, 2014 8:39 AM - vikas

nice

Tuesday, September 30, 2014 1:56 PM - 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...

Wednesday, November 12, 2014 10:56 AM - Jomon John

It's good.
Thank you....
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 some may not be published. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Not relevant to the article
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam
  • Anything in a language I don't understand including gibberish.

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

Allen Michaels 12/17/2014 4:37 PM
In response to Cascading DropDownLists with jQuery and ASP.NET
Fantastic thank you so much!...

Emily 12/17/2014 12:36 PM
In response to Parameterized IN clauses with ADO.NET and LINQ
Thanks, very helpful!!!! Can i use this for multiple in's ????? IN (.....) and IN(...) and IN...

sss 12/16/2014 3:06 PM
In response to Solving the Operation Must Use An Updateable Query error
good...

Gjuro 12/15/2014 10:30 PM
In response to Examining the Edit Methods and Edit View
You have one fromr (and it should be from, I suppose). :-)...

Gjuro 12/15/2014 10:27 PM
In response to Adding Search
Hi, thnx for all this C#->VB translations. Yet, the following code block is (slightly) in error it a...

Scot 12/14/2014 1:39 PM
In response to Entity Framework 6 Recipe - Alphabetical Paging In ASP.NET MVC
Thanks,Mike I found solution....

Gjuro 12/13/2014 10:52 PM
In response to Accessing Your Model's Data from a Controller
The article mentions "Creating an Entity Framework Data Model for an ASP.NET MVC Application" (at is...

Samuel 12/13/2014 8:40 AM
In response to Displaying The First n Characters Of Text
I have failed to use the extension because it throws an error that it doesn't recognise the chop be...

Ignas 12/12/2014 5:11 PM
In response to Cleaner Conditional HTML Attributes In Razor Web Pages
Any suggestions for Html Helper elements with HtmlAttributes, when you need to conditionally set it...

Gautam 12/11/2014 8:50 PM
In response to Validation In Razor Web Pages 2
Hi Mike Is this required for V3, non html helper input...