3

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
Tim Boland
  • 10,127
  • 8
  • 27
  • 25

4 Answers4

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

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

Greg
  • 16,540
  • 9
  • 51
  • 97
3

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.

Clyde
  • 8,017
  • 11
  • 56
  • 87
0
Response.Write(DateTime.Parse(dr("CreateDate").ToString()).ToString("MM/dd/yyyy"))
Geoff
  • 9,340
  • 7
  • 38
  • 48
0

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
stephenbayer
  • 12,373
  • 15
  • 63
  • 98