I am using JQUERY ajax to call an MVC method:
JQUERY:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Home/GetApplication/" + id,
dataType: "json",
data: '',
timeout: 10000,
success: function (obj) {
$('#Name').val(obj.Name);
$('#ApplicationIdentifier').val(obj.ApplicationIdentifier);
$('#Frequency').val(obj.FrequencyValue);
var d = new Date(parseInt(obj.BaseDate.substr(6)));
$('#BaseDate').val(d.getMonth() + '/' + d.getDay() + '/' + d.getFullYear());
},
error: function () {
return;
}
});
MVC Method:
[HttpPost]
public ActionResult GetApplication(int id)
{
return Json(new Application
{
Name = "Testing",
ApplicationIdentifier = "123ABC",
FrequencyValue = 1,
FrequencyType = 1,
BaseDate = DateTime.Now
});
}
All of this works fine except the date appearing on my form is 4/5/2011 and it should be 5/13/2011. Am I missing something here? Thanks.
Working with dates and times between JavaScript and .NET can be difficult, and we usually handle this in our applications by always making the interaction in Unix Time:
Now that your JavaScript application has the Unix Time, it can easily be converted to a Date (you are also formatting your date string incorrectly):
Also, I wouldn’t return your EF entity to the front-end, only give it what it really needs. By using a
dynamicobject, your types can be inferred here, and BaseDate becomes whatever type you give it: