Tuesday, November 17, 2009

ASP.NET - datetime format

Below listed the date time format you will frequently use in your application. You may replace the space in between the .ToString() to seperator instead of black space e.g. dash, slash, etc
DateTime d = DateTime.Now;
Response.Write("<br />today: " + d + "<br />");
//today: 11/18/2009 6:03:02 PM
Response.Write("<br />today: " + d.ToShortTimeString() + "<br />");
//today: 6:03 PM
Response.Write("<br />day: " + d.ToString("d dd ddd dddd") + "<br />");
//day: 18 18 Wed Wednesday
Response.Write("<br />month: " + d.ToString("M MM MMM MMMM") + "<br />");
//month: 11 11 Nov November
Response.Write("<br />year: " + d.ToString("y yy yyy yyyy") + "<br />");
//year: 9 09 2009 2009
Response.Write("<br />hour: " + d.ToString("h hh H HH") + "<br />");
//hour: 6 06 18 18
Response.Write("<br />minute: " + d.ToString("m mm") + "<br />");
//minute: 3 03
Response.Write("<br />second: " + d.ToString("s ss") + "<br />");
//second: 2 02
Response.Write("<br />AM/PM: " + d.ToString("t tt") + "<br />");
//AM/PM: P PM

No comments:

Post a Comment