Display DateTime in different formats

Lakhwinder.ghuman
Posted by Lakhwinder.ghuman under C# category on | Points: 40 | Views : 3433
Display Date Time according to your requirements with the help of DateTime.Tostring() method as:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Different Formats to use DateTime");
DateTime dt = DateTime.Now;
String strGhuman = "";
strGhuman = dt.ToString("MM/dd/yyyy");
Response.Write("<BR/>Format 1: " + strGhuman);

strGhuman = dt.ToString("dddd, dd MMMM yyyy");
Response.Write("<BR/>Format 2: "+strGhuman);

strGhuman = dt.ToString("dddd, dd MMMM yyyy HH:mm");
Response.Write("<BR/>Format 3: " + strGhuman);

strGhuman = dt.ToString("dddd, dd MMMM yyyy hh:mm tt");
Response.Write("<BR/>Format 4: " + strGhuman);

strGhuman = dt.ToString("dddd, dd MMMM yyyy H:mm");
Response.Write("<BR/>Format 5: " + strGhuman);

strGhuman = dt.ToString("dddd, dd MMMM yyyy h:mm tt");
Response.Write("<BR/>Format 6: " + strGhuman);

strGhuman = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss");
Response.Write("<BR/>Format 7: " + strGhuman);

strGhuman = dt.ToString("MM/dd/yyyy HH:mm");
Response.Write("<BR/>Format 8: " + strGhuman);

strGhuman = dt.ToString("MM/dd/yyyy hh:mm tt");
Response.Write("<BR/>Format 9: " + strGhuman);

strGhuman = dt.ToString("MM/dd/yyyy H:mm");
Response.Write("<BR/>Format 10: " + strGhuman);

strGhuman = dt.ToString("MM/dd/yyyy h:mm tt");
Response.Write("<BR/>Format 11: " + strGhuman);

strGhuman = dt.ToString("MM/dd/yyyy HH:mm:ss");
Response.Write("<BR/>Format 12: " + strGhuman);

strGhuman = dt.ToString("MMMM dd"); // July 21
Response.Write("<BR/>Format 13: " + strGhuman);

strGhuman = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK");
Response.Write("<BR/>Format 14: " + strGhuman);

strGhuman = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'");
Response.Write("<BR/>Format 15: " + strGhuman);

strGhuman = dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");
Response.Write("<BR/>Format 16: " + strGhuman);

strGhuman = dt.ToString("HH:mm");
Response.Write("<BR/>Format 17: " + strGhuman);

strGhuman = dt.ToString("hh:mm tt");
Response.Write("<BR/>Format 18: " + strGhuman);

strGhuman = dt.ToString("H:mm");
Response.Write("<BR/>Format 19: " + strGhuman);

strGhuman = dt.ToString("h:mm tt");
Response.Write("<BR/>Format 20: " + strGhuman);

strGhuman = dt.ToString("HH:mm:ss");
Response.Write("<BR/>Format 21: " + strGhuman);

strGhuman = dt.ToString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'");
Response.Write("<BR/>Format 22: " + strGhuman);

strGhuman = dt.ToString("dddd, dd MMMM yyyy HH:mm:ss");
Response.Write("<BR/>Format 23: " + strGhuman);

strGhuman = dt.ToString("yyyy MMMM");
Response.Write("<BR/>Format 24: " + strGhuman);
}

Comments or Responses

Posted by: T.saravanan on: 5/8/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi,

Kindly post your code inside the code tag.

Login to post response