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,001
  • 8
  • 25
  • 25

4 Answers4

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

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

Greg
  • 16,217
  • 9
  • 50
  • 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
  • 7,949
  • 10
  • 54
  • 84
0
Response.Write(DateTime.Parse(dr("CreateDate").ToString()).ToString("MM/dd/yyyy"))
Geoff
  • 9,320
  • 7
  • 37
  • 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,104
  • 15
  • 62
  • 98