admin管理员组

文章数量:1023195

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?

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?

Share Improve this question edited Mar 22, 2010 at 9:12 jitter 54.7k11 gold badges113 silver badges130 bronze badges asked Mar 22, 2010 at 9:09 Brian ScottBrian Scott 9,3717 gold badges48 silver badges68 bronze badges 1
  • Could you add a sample date at least. You can't expect people to investigate what kind of date is affected by this – jitter Commented Mar 22, 2010 at 9:12
Add a ment  | 

3 Answers 3

Reset to default 1

After some initial confusion (answers by me and Andy E and mainly my ments 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 es 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 es 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

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;
}

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).

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?

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?

Share Improve this question edited Mar 22, 2010 at 9:12 jitter 54.7k11 gold badges113 silver badges130 bronze badges asked Mar 22, 2010 at 9:09 Brian ScottBrian Scott 9,3717 gold badges48 silver badges68 bronze badges 1
  • Could you add a sample date at least. You can't expect people to investigate what kind of date is affected by this – jitter Commented Mar 22, 2010 at 9:12
Add a ment  | 

3 Answers 3

Reset to default 1

After some initial confusion (answers by me and Andy E and mainly my ments 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 es 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 es 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

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;
}

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).

本文标签: javascriptHow do I parse a UTC date format string to local date timeStack Overflow