Possible Duplicate:
DateTime.ParseExact format string
How do I convert a string to a DateTime object?
Example:
Sun Oct 07 2012 00:00:00 GMT+0500 (Pakistan Standard Time)
I have tried, DateTime.Parse, Convert.TODateTime, etc. None work. I get an error that it is not a valid DateTime string.
Here is how I am sending datetime to MVC controller’s action method from jquery:
$.ajax({
url: '@Url.Action("actionMethodName", "controllerName")',
type: "GET",
cache: false,
data: {
startDate: start.toLocaleString(),
endDate: end.toLocaleString()
},
success: function (data) {
}
});
I need to be able to get the datetime back in the controller action method:
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
}
I would recommend you to use the
.toJSON()method on your javascriptDateinstances in order to serialize them as ISO 8601 format:Now you don’t need to be parsing anything in your controller, you will be working with dates directly: