views:

581

answers:

3

I'm currently using the jQuery fullcalendar plugin but I've came across an issue regarding daylight savings time in Britain. After the daylight savings take effect the hours are being displayed an hour out of phase.

The problem appears to be with the plugins parsing function. Can someone please provide me with the funciton to parse a UTC string which includes the 'Z' demonination into a local date time for display?

A: 

I assume you mean an ISO 8601 date string (UTC is a time standard, not a display format). I've used this function from delete.me.uk for a few years now, in a production environment with no issues testing it in BST/GMT (my local timezone).

Andy E
@jitter: you're downvoting me for a bug that's been fixed? (and now you've edited your comment, mine makes no sense...)
Andy E
@jitter: done. I can only provide you with my own experience using the function - using it in a production environment since 2006 to parse eBay API date strings. Tested in my local timezone, BST and GMT, and other timezones with no issues and none reported by my customers.
Andy E
After more investigation I discovered what really is wrong. Thus removed my misleading comments and took back the downvote
jitter
@jitter: thanks :-)
Andy E
A: 

This could work for you:

function parseDate(value) {
    var a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);

    if (a) {
        return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
    }

    return null;
}
Philippe Leybaert
A: 

After some initial confusion (answers by me and Andy E and mainly my comments to his answer) which you probably missed anyway, I discovered what seems to be the real cause of your problem.

The plugin internally uses the same code of the parseISO8601 function which was proposed by Andy E that comes from Parsing W3C's ISO 8601 Date/Times in JavaScript.

But although Andy E states that that code works without problems, the fullcalendar plugin, which uses that code too, seems to have a bug.

After looking more closely at the code, I noticed that the plugin seems to ignore the timezone you are in.

Compare these code snippets

Snippet from original code from delete.me.uk

if (d[14]) {
    offset = (Number(d[16]) * 60) + Number(d[17]);
    offset *= ((d[15] == '-') ? 1 : -1);
}

Code from fullcalendar.js

if (!ignoreTimezone) {
    if (m[14]) {
        offset = Number(m[16]) * 60 + Number(m[17]);
        offset *= m[15] == '-' ? 1 : -1;
    }
    offset -= date.getTimezoneOffset();
}

As you can see the plugin only handles the timezone if ignoreTimezone is set to false. But that simply is never the case. parseISO8601() in this plugin is always called with ignoreTimezone set to true.

Thus I bet the bug comes from this and you should consider contacting the author of the plugin. But first you should verify if the date gets parsed correctly if you set ignoreTimezone to false in the plugin code

jitter