Chrome interprets ISO time without Z as UTC; C# issue

后端 未结 5 2087
野性不改
野性不改 2020-12-11 02:23

Run this jsfiddle: http://jsfiddle.net/E9gq9/7/ on Chrome, FF, and IE and you get:

Chrome:

Chrome http://images.devs-on.net/Image/vBTz86J0f4o8zlL3-Region.png

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 03:01

    David Hammond's answer is great but doesn't play all the tricks; so here is a modified version:

    • allows for fractional part in date/time string
    • allows for optional seconds in date/time string
    • considers crossing over daylight-saving time
    appendTimezone = function (/*String*/ d) {
        // check for ISO 8601 date-time string (seconds and fractional part are optional)
        if (d.search(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1-3})?)?$/) == 0) {
            var pad = function (num) {
                norm = Math.abs(Math.floor(num));
                return (norm < 10 ? '0' : '') + norm;
            },
            tzo = -new Date(d).getTimezoneOffset(),
            sign = tzo >= 0 ? '+' : '-';
    
            var adjusted = d + sign + pad(tzo / 60) + ':' + pad(tzo % 60);
    
            // check whether timezone offsets are equal;
            // if not then the specified date is just within the hour when the clock
            // has been turned forward or back
            if (-new Date(adjusted).getTimezoneOffset() != tzo) {
                // re-adjust
                tzo -= 60;
                adjusted = d + sign + pad(tzo / 60) + ':' + pad(tzo % 60);
            }
    
            return adjusted;
        } else {
            return d;
        }
    }
    
    parseDate = function (/*String*/ d) {
        return new Date(appendTimezone(d));
    }
    

提交回复
热议问题