Convertir la fecha de jquery en DateTime

I use the jquery ui controls.
I have a datepicker and I retrieve the date as follows:

var sDate = $("#startDatePicker").datepicker('getDate'); 
var eDate = $("#endDatePicker").datepicker('getDate');

which as an example for sDate returns

Tue Aug 14 00:00:00 UTC+0200 2012

I call the code from the web page as follows

$.ajax(
{
  url: '@Url.Action("LogsView","Home")',
  type: 'POST',
  data: { startDate: sDate, endDate: eDate },
  success: function(result) { alert(result); },                        
  error: function(param1) { alert(param1); }
});

I have a controller with the following action

public JsonResult LogsView(DateTime startDate, DateTime endDate)
{   
  var items = FetchItems(startDate, endDate);
  return Json(items, JsonRequestBehavior.AllowGet);
}

When I run it, part of the error that is returned is as follows (when vied in fiddler):

The parameters dictionary contains a null entry for parameter 'startDate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.JsonResult LogsView(System.DateTime, System.DateTime)' ..

I've check my params sent via fiddler and it is as follows startDate=&endDate=

Anyone know how to format and pass in the date correctly (I assume this is where the error lies)?

preguntado el 28 de agosto de 12 a las 11:08

3 Respuestas

Necesitaba agregar .toJSON() a $("#startDatePicker").datepicker('getDate')
es decir, $("#startDatePicker").datepicker('getDate').toJSON()

if you get an error on the toJSON() function then try

var sd = $("#startDatePicker").datepicker('getDate');
var sDate = $.datepicker.formatDate('dd-MM-yy', sd);

Respondido el 03 de Septiembre de 12 a las 14:09

You need to convert the date to string before you send it off to $.ajax() method.

Respondido 28 ago 12, 11:08

You can this method to convert date to string:

function ShowUTCDate()
{
    var dNow = new Date();
    var utc = new Date(dNow.getTime() + dNow.getTimezoneOffset() * 60000);
    var utcdate= (utc.getMonth()+1) + '/' + utc.getDate() + '/' + utc.getFullYear() + ' ' + utc.getHours() + ':' + utc.getMinutes();
    return utcdate;
}

Respondido 06 Abr '17, 14:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.