11

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); });
    },
1

5 Answers 5

9

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.
Sign up to request clarification or add additional context in comments.

Comments

3

try to replace

start.toUTCString()

with

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

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

Comments

1

When I use the function .toUTCString() under IE, I had trouble with UTC vs GMT and also with the leading zeros.

This is what I use to send UTC date to ASP.NET MVC :

function getUTCString (date) {

    function pad(n) { return n < 10 ? "0" + n : n }    

    var weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    return weekday[date.getUTCDay()]
        + ", " + pad(date.getUTCDate())
        + " "  + month[date.getUTCMonth()]
        + " "  + date.getUTCFullYear()
        + " "  + pad(date.getUTCHours())
        + ":"  + pad(date.getUTCMinutes())   
        + ":" + pad(date.getUTCSeconds())
        + " GMT"; 
};

Comments

0

I've also encountered this weird situation. Replacing UTC to GMT in the date string solved my problem but I've wanted to look for another way (replacing strings sometimes cause problems :)).

I've come up with the following solution which worked well in IE, Chrome, and Firefox:

var myUTCDateString = (function(d) {
          return d.getUTCFullYear() + "-" + (d.getUTCMonth() +1) + "-" +  d.getUTCDate() + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" + d.getUTCSeconds();
     })(new Date());

myUTCDateString is something like "2012-1-3 21:47:49".

Comments

0

I've found here and modified a helper:

// #region Date.prototype.toISONonUtcString
// add Date prototype toISONonUtcString function if it doesn't yet exist
if ($.isFunction(Date.prototype.toISONonUtcString) === false) {
Date.prototype.toISONonUtcString = function () {
    var pad = function (n, places) {
        n = n.toString();
        for (var i = n.length; i < places; i++) {
            n = "0" + n;
        }
        return n;
    };
    var d = this;
    return pad(d.getFullYear(), 4) + '-' + pad(d.getMonth() + 1, 2) + '-' + pad(d.getDate(), 2) + 'T' +
                pad(d.getHours(), 2) + ':' + pad(d.getMinutes(), 2) + ':' + pad(d.getSeconds(), 2) + '.' +
                pad(d.getMilliseconds(), 3);
};
}
// #endregion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.