字符串格式日期 - C# 或 VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/128349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String Format Date - C# or VB.NET
提问by Tim Boland
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
回答by Greg
string.Format( "{0:MM/dd/yy}", dr("CreateDate") )
Edit: If dr("CreateDate") is DBNull, this returns "".
编辑:如果 dr("CreateDate") 是 DBNull,则返回""。
回答by Clyde
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.
请参阅 MSDN 文档了解 DateTime 数据类型提供的其他功能,包括通过“ToString”功能提供的自定义格式。
回答by stephenbayer
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
回答by Geoff
Response.Write(DateTime.Parse(dr("CreateDate").ToString()).ToString("MM/dd/yyyy"))

