简体   繁体   English

字符串格式日期 - C#或VB.NET

[英]String Format Date - C# or VB.NET

Date coming out of a database, need to format as "mm/dd/yy" 从数据库出来的日期,需要格式化为“mm / dd / yy”

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

Response.Write(dr("CreateDate"))

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

编辑:如果dr(“CreateDate”)是DBNull,则返回“”。

Convert.ToDateTime(dr("CreateDate")).ToShortDate() 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. 有关DateTime数据类型中可用的其他函数,请参阅MSDN文档,包括通过“ToString”函数提供的自定义格式。

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 //我还会在做之前检查它是不是dbnull

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM