Question

Date coming out of a database, need to format as "mm/dd/yy"

For Each dr as DataRow in ds.Tables(0).Rows

Response.Write(dr("CreateDate"))

Next
Was it helpful?

Solution

string.Format( "{0:MM/dd/yy}", dr("CreateDate") )

Edit: If dr("CreateDate") is DBNull, this returns "".

OTHER TIPS

Convert.ToDateTime(dr("CreateDate")).ToShortDate()

See the MSDN docs for other functions available from the DateTime datatype, including custom formats available through the 'ToString' function.

Response.Write(DateTime.Parse(dr("CreateDate").ToString()).ToString("MM/dd/yyyy"))

Easy:

((DateTime)dr["CreateDate"]).ToString("MM/dd/yyyy")

// I would also check that it isn't dbnull before doing it though

if (! DBNull.Value.Equals(dr["CreateDate"])) // blah blah
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top