Click here to Skip to main content
Click here to Skip to main content

Formats for DateTime.ToString()

, 20 Jul 2007
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

S.Vinothkumar
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

 
SuggestionMessage Automatically Removed PinmemberAyesha Sharma18-Apr-14 1:31 
GeneralMy vote of 3 PinmemberYogesh Potdar14-May-13 1:30 
GeneralMy vote of 3 Pinmemberd347hm4n26-Jun-12 6:46 
GeneralMy vote of 1 PinmemberMember 782321127-Apr-12 1:52 
GeneralMy vote of 1 PinmemberVMykyt16-Jul-09 21:54 
GeneralEasy for the Lazy Pinmemberhairry_p0tter14-Oct-08 2:32 
GeneralUnnecessary apostrophes PinmemberPIEBALDconsult21-Jul-07 10:11 
QuestionReally!? PinmvpMark Nischalke21-Jul-07 9:35 
GeneralBetter Reference at Geekzilla Pinmemberdelyk21-Jul-07 8:35 
QuestionWhy? Pinmembermav.northwind21-Jul-07 8:29 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 21 Jul 2007
Article Copyright 2007 by S.Vinothkumar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid