You can display a DateTime with custom format using DateTime.ToString() or String.Format() methods in .NET. The custom format string follows a set of naming convention such as y for year, M for month, d for day, h for hour 12, m for minute, s for second, and z for time zone. Let's create a simple ASP.NET page to exam the DateTime format string:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string dtString = string.Empty;
DateTime dt = DateTime.Now;
StringBuilder sb = new StringBuilder();
sb.Append("y yy yyy yyyy \t--- (Year) " + dt.ToString("y yy yyy yyyy") + "<br>");
sb.Append("M MM MMM MMMM \t--- (Month) " + dt.ToString("M MM MMM MMMM") + "<br>");
sb.Append("d dd ddd dddd \t--- (Day) " + dt.ToString("d dd ddd dddd") + "<br>");
sb.Append("h hh H HH \t--- (Hour) " + dt.ToString("h hh H HH") + "<br>");
sb.Append("m mm \t\t--- (Minute) " + dt.ToString("m mm") + "<br>");
sb.Append("z zz zzz \t--- (Zone) " + dt.ToString("z zz zzz") + "<br>");
sb.Append("d \t--- " + dt.ToString("d") + "<br>");
sb.Append("D \t--- " + dt.ToString("D") + "<br>");
sb.Append("f \t--- " + dt.ToString("f") + "<br>");
sb.Append("F \t--- " + dt.ToString("F") + "<br>");
sb.Append("g \t--- " + dt.ToString("g") + "<br>");
sb.Append("G \t--- " + dt.ToString("G") + "<br>");
sb.Append("m \t--- " + dt.ToString("m") + "<br>");
sb.Append("r \t--- " + dt.ToString("r") + "<br>");
sb.Append("s \t--- " + dt.ToString("s") + "<br>");
sb.Append("u \t--- " + dt.ToString("u") + "<br>");
sb.Append("U \t--- " + dt.ToString("U") + "<br>");
sb.Append("y \t--- " + dt.ToString("y") + "<br>");
sb.Append("yyyy-MM-dd \t\t--- " + dt.ToString("yyyy-MM-dd") + "<br>");
sb.Append("yyyy-MM-dd HH:mm \t--- " + dt.ToString("yyyy-MM-dd HH:mm") + "<br>");
sb.Append("yyyy-MM-dd HH:mm:ss \t--- " + dt.ToString("yyyy-MM-dd HH:mm:ss") + "<br>");
sb.Append("yyyy-MM-dd HH:mm tt \t--- " + dt.ToString("yyyy-MM-dd HH:mm tt") + "<br>");
sb.Append("yyyy-MM-dd H:mm \t--- " + dt.ToString("yyyy-MM-dd H:mm") + "<br>");
sb.Append("yyyy-MM-dd h:mm \t--- " + dt.ToString("yyyy-MM-dd h:mm") + "<br>");
sb.Append("ddd, dd MMM yyyy HH:mm \t--- " + dt.ToString("ddd, dd MMM yyyy HH:mm") + "<br>");
sb.Append("dddd, dd MMMM yyyy HH:mm \t--- " + dt.ToString("dddd, dd MMMM yyyy HH:mm") + "<br>");
lblDateTimeFormat.Text = sb.ToString();
}
}
Result:
y yy yyy yyyy --- (Year) 6 06 2006 2006
M MM MMM MMMM --- (Month) 1 01 Jan January
d dd ddd dddd --- (Day) 18 18 Wed Wednesday
h hh H HH --- (Hour) 9 09 21 21
m mm --- (Minute) 12 12
z zz zzz --- (Zone) -5 -05 -05:00
d --- 1/18/2006
D --- Wednesday, January 18, 2006
f --- Wednesday, January 18, 2006 9:12 PM
F --- Wednesday, January 18, 2006 9:12:36 PM
g --- 1/18/2006 9:12 PM
G --- 1/18/2006 9:12:36 PM
m --- January 18
r --- Wed, 18 Jan 2006 21:12:36 GMT
s --- 2006-01-18T21:12:36
u --- 2006-01-18 21:12:36Z
U --- Thursday, January 19, 2006 2:12:36 AM
y --- January, 2006
yyyy-MM-dd --- 2006-01-18
yyyy-MM-dd HH:mm --- 2006-01-18 21:12
yyyy-MM-dd HH:mm:ss --- 2006-01-18 21:12:36
yyyy-MM-dd HH:mm tt --- 2006-01-18 21:12 PM
yyyy-MM-dd H:mm --- 2006-01-18 21:12
yyyy-MM-dd h:mm --- 2006-01-18 9:12
ddd, dd MMM yyyy HH:mm --- Wed, 18 Jan 2006 21:12
dddd, dd MMMM yyyy HH:mm --- Wednesday, 18 January 2006 21:12
In reality a DateTime value is often passed through with a formatted string, and is converted back to DateTime at some point. The safest way for parsing a DateTime string is using the TryParseExact method:
string dtString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
DateTime curDate;
DateTime.TryParseExact(dtString, "yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.None, out curDate);