Formats for DateTime.ToString()






2.66/5 (30 votes)
Jul 21, 2007

293426
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.