String formatting date - C # or VB.NET

Date of exit from the database, must be formatted as "mm / dd / yy"

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

Response.Write(dr("CreateDate"))

Next
+3
source share
4 answers
string.Format( "{0:MM/dd/yy}", dr("CreateDate") )

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

+11
source

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

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

+3
source
Response.Write(DateTime.Parse(dr("CreateDate").ToString()).ToString("MM/dd/yyyy"))
0

:

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

// , dbnull, ,

if (! DBNull.Value.Equals(dr["CreateDate"])) // blah blah
0

Source: https://habr.com/ru/post/1697474/


All Articles