【发布时间】:2012-10-09 14:17:32
【问题描述】:
如何将字符串转换为 DateTime 对象?
例子:
2012 年 10 月 7 日星期日 00:00:00 GMT+0500(巴基斯坦标准时间)
我已经尝试过,DateTime.Parse、Convert.TODateTime 等。没有工作。我收到一个错误,指出它不是有效的 DateTime 字符串。
这是我如何从 jquery 向 MVC 控制器的操作方法发送日期时间:
$.ajax({
url: '@Url.Action("actionMethodName", "controllerName")',
type: "GET",
cache: false,
data: {
startDate: start.toLocaleString(),
endDate: end.toLocaleString()
},
success: function (data) {
}
});
我需要能够在控制器操作方法中取回日期时间:
public JsonResult actionMethodName(string startDate, string endDate)
{
if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
{
var start = DateTime.Parse(startDate); //Get exception here
var end = DateTime.Parse(endDate); //Get exception here
}
//Rest of the code
}
【问题讨论】:
-
What have you tried?你被困在哪里了?你还有多少其他的例子?
-
DateTime.Parse/DateTime.TryParse
-
@Zdeslav Vojkovic。两者都不起作用
-
DateTime convertDate = DateTime.Parse(dateString);
-
我假设你是从 JS 得到这个的?见:stackoverflow.com/questions/12675421/…
标签: c# asp.net-mvc-3 jquery