Click here to Skip to main content
15,885,141 members
Articles / Programming Languages / C#
Article

Formats for DateTime.ToString()

Rate me:
Please Sign up or sign in to vote.
2.66/5 (30 votes)
20 Jul 2007 291.9K   29   12
Formats for DateTime.ToString()

Introduction

Here is the lot of formats are given for how to use the DateTime.ToString() in our C# Project.

Background

The DateTime class is most usefull for our time based programs. But we dont' know how to use the DateTime.ToString() function with appropriate formats. Here I am giving some formats for using DateTime.ToString().

Using the code

In our C# Program, we should declare the datetime and write the code as follows,

The result also given in right side...

//
// Any source code blocks look like this
//

DateTime dt = DateTime.Now;
String strDate="";
strDate = dt.ToString("MM/dd/yyyy");   // 07/21/2007 
strDate = dt.ToString("dddd, dd MMMM yyyy");   //Saturday, 21 July 2007
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm"); // Saturday, 21 July 2007 14:58
strDate = dt.ToString("dddd, dd MMMM yyyy hh:mm tt"); // Saturday, 21 July 2007 03:00 PM
strDate = dt.ToString("dddd, dd MMMM yyyy H:mm"); // Saturday, 21 July 2007 5:01 
strDate = dt.ToString("dddd, dd MMMM yyyy h:mm tt"); // Saturday, 21 July 2007 3:03 PM
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); // Saturday, 21 July 2007 15:04:10
strDate = dt.ToString("MM/dd/yyyy HH:mm"); // 07/21/2007 15:05
strDate = dt.ToString("MM/dd/yyyy hh:mm tt"); // 07/21/2007 03:06 PM
strDate = dt.ToString("MM/dd/yyyy H:mm"); // 07/21/2007 15:07
strDate = dt.ToString("MM/dd/yyyy h:mm tt"); // 07/21/2007 3:07 PM
strDate = dt.ToString("MM/dd/yyyy HH:mm:ss"); // 07/21/2007 15:09:29
strDate = dt.ToString("MMMM dd"); // July 21
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"); // 2007-07-21T15:11:19.1250000+05:30    
strDate = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"); // Sat, 21 Jul 2007 15:12:16 GMT
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); // 2007-07-21T15:12:57
strDate = dt.ToString("HH:mm"); // 15:14
strDate = dt.ToString("hh:mm tt"); // 03:14 PM
strDate = dt.ToString("H:mm"); // 5:15
strDate = dt.ToString("h:mm tt"); // 3:16 PM
strDate = dt.ToString("HH:mm:ss"); // 15:16:29
strDate = dt.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'"); // 2007-07-21 15:17:20Z
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss"); // Saturday, 21 July 2007 15:17:58
strDate = dt.ToString("yyyy MMMM"); // 2007 July



Hence we can format and getting the datetime value.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Hi Viewers,

I wish to all. This is Vinoth. This is where I try to condense everything that you need to know about me.

Blog:

visit my blog

Interests:

I'm passionate about a great many things and continually learning about the things that interest me. They are wearable computers, User Interface Design, Artificial life, Industrial music.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Jim_Snyder20-Feb-19 8:16
professionalJim_Snyder20-Feb-19 8:16 
QuestionDaniel Aaron Sessum Pin
Member 1363150718-Jan-18 17:24
Member 1363150718-Jan-18 17:24 
GeneralMy vote of 3 Pin
Yogesh Potdar14-May-13 0:30
Yogesh Potdar14-May-13 0:30 
GeneralMy vote of 3 Pin
d347hm4n26-Jun-12 5:46
d347hm4n26-Jun-12 5:46 
GeneralMy vote of 1 Pin
Member 782321127-Apr-12 0:52
Member 782321127-Apr-12 0:52 
GeneralMy vote of 1 Pin
VMykyt16-Jul-09 20:54
VMykyt16-Jul-09 20:54 
GeneralEasy for the Lazy Pin
hairry_p0tter14-Oct-08 1:32
hairry_p0tter14-Oct-08 1:32 
GeneralUnnecessary apostrophes Pin
PIEBALDconsult21-Jul-07 9:11
mvePIEBALDconsult21-Jul-07 9:11 
QuestionReally!? Pin
Not Active21-Jul-07 8:35
mentorNot Active21-Jul-07 8:35 
GeneralBetter Reference at Geekzilla Pin
delyk21-Jul-07 7:35
delyk21-Jul-07 7:35 
QuestionWhy? Pin
mav.northwind21-Jul-07 7:29
mav.northwind21-Jul-07 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.