我正在尝试将数字月份作为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"
我做错了什么?
答案 0 :(得分:7)
为什么不简单
dateToReturn = newDate.Month.ToString();
或
dateToReturn = newDate.Month.ToString("D2");
将月份部分格式化为前导零。
答案 1 :(得分:0)
dateToReturn = String.Format("{0:M}",Convert.toDateTime(newDate));