Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date javascript object from IE cannot be automatically bound to Datetime in ASP.NET MVC

I have a site that uses a jquery calendar to display events. I have noticed than when using the system from within IE (all versions) ASP.NET MVC will fail to bind the datetime to the action that send back the correct events.

The sequence of events goes as follows.

  • Calendar posts to server to get events
  • Server ActionMethod accepts start and end date, automatically bound to datetime objects

In every browser other than IE the start and end date come through as:

Mon, 10 Jan 2011 00:00:00 GMT

When IE posts the date, it comes through as

Mon, 10 Jan 2011 00:00:00 UTC

ASP.NET MVC 2 will then fail to automatically bind this to the action method parameter.

Is there a reason why this is happening? The code that posts to the server is as follows:

data: function (start, end, callback) {
        $.post('/tracker/GetTrackerEvents', { start: start.toUTCString(), end: end.toUTCString() }, function (result) { callback(result); });
    },
like image 488
Sergio Avatar asked Jan 14 '11 11:01

Sergio


2 Answers

I ran into the same problem and I solved it using JavaScript Date()'s .toISOString() function (rather than doing a string replace).

See doc here.

DateTime and DateTimeOffset will process this correctly regardless of the broswer sending it (in my limited IE, Firefox, and Chrome testing).

So, I send the date to the controller like so (this is my JS object):

var dateObject = new Date();
dateObject.toISOString()

And the server can parse the data like so (.NET - in the controller):

DateTimeOffset timeInGMT = DateTime.Parse(dateString).ToUniversalTime(); //for the time in GMT
DateTime timeOnClient = DateTime.Parse(dateString); //for time as it was set on the client.
like image 176
MattW Avatar answered Oct 05 '22 14:10

MattW


try to replace

start.toUTCString()

with

start.toUTCString().replace('UTC','GMT')

and see if that doesn't fix the problem for you :)

like image 33
Martin Jespersen Avatar answered Oct 05 '22 15:10

Martin Jespersen