Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Format Date - C# or VB.NET

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
like image 252
Tim Boland Avatar asked Jan 20 '26 02:01

Tim Boland


2 Answers

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

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

like image 70
Greg Avatar answered Jan 22 '26 15:01

Greg


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.

like image 27
Clyde Avatar answered Jan 22 '26 16:01

Clyde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!