Click here to Skip to main content
12,891,469 members (50,676 online)
Click here to Skip to main content
Add your own
alternative version

Stats

330.1K views
29 bookmarked
Posted 20 Jul 2007

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:

<a href = http://vinothnat.blogspot.com/> 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.

You may also be interested in...

Comments and Discussions

 
SuggestionMessage Automatically Removed Pin
18-Apr-14 0:31
memberAyesha Sharma18-Apr-14 0:31 
GeneralMy vote of 3 Pin
Yogesh Potdar14-May-13 0:30
memberYogesh Potdar14-May-13 0:30 
GeneralMy vote of 3 Pin
d347hm4n26-Jun-12 5:46
memberd347hm4n26-Jun-12 5:46 
GeneralMy vote of 1 Pin
Member 782321127-Apr-12 0:52
memberMember 782321127-Apr-12 0:52 
GeneralMy vote of 1 Pin
VMykyt16-Jul-09 20:54
memberVMykyt16-Jul-09 20:54 
GeneralEasy for the Lazy Pin
hairry_p0tter14-Oct-08 1:32
memberhairry_p0tter14-Oct-08 1:32 
GeneralUnnecessary apostrophes Pin
PIEBALDconsult21-Jul-07 9:11
memberPIEBALDconsult21-Jul-07 9:11 
QuestionReally!? Pin
Mark Nischalke21-Jul-07 8:35
mvpMark Nischalke21-Jul-07 8:35 
GeneralBetter Reference at Geekzilla Pin
delyk21-Jul-07 7:35
memberdelyk21-Jul-07 7:35 
QuestionWhy? Pin
mav.northwind21-Jul-07 7:29
membermav.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.

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