从月份返回数字月份作为字符串

时间:2013-05-29 12:54:47

标签: c# asp.net datetime tostring date-formatting

我正在尝试将数字月份作为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"

我做错了什么?

2 个答案:

答案 0 :(得分:7)

为什么不简单

dateToReturn = newDate.Month.ToString();

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

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

答案 1 :(得分:0)

dateToReturn = String.Format("{0:M}",Convert.toDateTime(newDate));