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...
DateTime dt = DateTime.Now;
String strDate="";
strDate = dt.ToString("MM/dd/yyyy");
strDate = dt.ToString("dddd, dd MMMM yyyy");
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm");
strDate = dt.ToString("dddd, dd MMMM yyyy hh:mm tt");
strDate = dt.ToString("dddd, dd MMMM yyyy H:mm");
strDate = dt.ToString("dddd, dd MMMM yyyy h:mm tt");
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss");
strDate = dt.ToString("MM/dd/yyyy HH:mm");
strDate = dt.ToString("MM/dd/yyyy hh:mm tt");
strDate = dt.ToString("MM/dd/yyyy H:mm");
strDate = dt.ToString("MM/dd/yyyy h:mm tt");
strDate = dt.ToString("MM/dd/yyyy HH:mm:ss");
strDate = dt.ToString("MMMM dd");
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK");
strDate = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'");
strDate = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");
strDate = dt.ToString("HH:mm");
strDate = dt.ToString("hh:mm tt");
strDate = dt.ToString("H:mm");
strDate = dt.ToString("h:mm tt");
strDate = dt.ToString("HH:mm:ss");
strDate = dt.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'");
strDate = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss");
strDate = dt.ToString("yyyy MMMM");
Hence we can format and getting the datetime value.