Date.getTime() returns NaN for ISO/Twitter API IE11 中的日期

Date.getTime() returns NaN for ISO/Twitter API dates in IE11

这是my code:

var date = "2014-07-23T15:23:12+0000";
var ts = new Date(date).getTime();
console.log(ts);

为什么IE11会打印NaN

Firefox/Chrome/and其他浏览器打印没有问题1406128992000.

引自 ECMAScript 语言规范,第 Date Time String Format 节:

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
...
Z is the time zone offset specified as "Z" (for UTC) or either "+" or "-" followed by a time expression HH:mm

显然,您需要在时区指示符中添加一个 :。这应该在 IE9 中工作:

var dateString = "2014-07-23T15:23:12+0000";
var dateStringISO = dateString.replace(/([+\-]\d\d)(\d\d)$/, ":");
var timestamp = new Date(dateStringISO).getTime();
console.log(dateString, dateStringISO, timestamp);

对于 Twitter 日期,您可以使用相同的策略:

var dateString = "Mon Jan 13 16:04:04 +0000 2014";
var dateStringISO = dateString.replace(/^... (...) (..) (........) (...)(..) (....)$/, function(match, month, date, time, tz1, tz2, year) {
  return year + "-" + {
    Jan: "01",
    Feb: "02",
    Mar: "03",
    Apr: "04",
    May: "05",
    Jun: "06",
    Jul: "07",
    Aug: "08",
    Sep: "09",
    Oct: "10",
    Nov: "11",
    Dec: "12"
  }[month] + "-" + date + "T" + time + tz1 + ":" + tz2;
});
var timestamp = new Date(dateStringISO).getTime();
console.log(dateString, dateStringISO, timestamp);