0

我试图从 DateTime 值中获取数字月份作为字符串,而不是获取 MonthName、dayOfMonth。这是我的代码:

string columnDate = "7/1/2003 12:00:00 AM";
string datePart = "M";
DateTime newDate = DateTime.Parse(columnDate); // 7/1/2003 12:00:00 AM as date 
dateToReturn = newDate.ToString(datePart); 
// dateToReturn is set to "July 01". It's supposed to be "7"

我究竟做错了什么?

4

2 回答 2

7

为什么不简单

dateToReturn = newDate.Month.ToString();

或者

dateToReturn = newDate.Month.ToString("D2");

将月份部分格式化为前导零。

于 2013-05-29T12:56:56.537 回答
0
dateToReturn = String.Format("{0:M}",Convert.toDateTime(newDate));
于 2013-05-29T12:58:33.867 回答