如何将ISO 8601格式的日期值转换为JavaScript中的日期对象?

时间:2009-01-31 09:40:41

标签: javascript date iso8601

我一直在尝试将日期值转换为更易读的格式。为此,我尝试使用JavaScript Date.parse()方法解析日期。然而,这对我所拥有的输入(例如:"2007-09-21T14:15:34.058-07:00")不起作用。最终目标是输出日期字符串,如"January 30th, 2008 @ 2:15PM"

有什么想法吗?

2 个答案:

答案 0 :(得分:20)

你应该使用datejs推荐的f3lix,但是我很无聊并把一个小对象扔到了一起,完全符合你的要求:

2012年9月25日:清除代码,允许非扩展格式,例如20120925T164740 + 0200

2011年12月1日:修复了月份字符串中的错误。八月失踪

var ISODate = {
  convert :
    function (input){
      if (!(typeof input === "string")) throw "ISODate, convert: input must be a string";
      var d = input.match(/^(\d{4})-?(\d{2})-?(\d{2})[T ](\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|(?:([+-])(\d{2}):?(\d{2})))$/i);
      if (!d) throw "ISODate, convert: Illegal format";
      return new Date(
        Date.UTC(
          d[1], d[2]-1, d[3],
          d[4], d[5], d[6], d[7] || 0 % 1 * 1000 | 0
        ) + (
          d[8].toUpperCase() === "Z" ? 0 :
            (d[10]*3600 + d[11]*60) * (d[9] === "-" ? 1000 : -1000)
        )
      );
    },
  format :
    function(date, utc){
      if (typeof date === "string") date = this.convert(date);
      if (!(date instanceof Date)) throw "ISODate, format: t is not a date object";

      var t={'FullYear':0, 'Month':0, 'Date':0, 'Hours':0, 'Minutes':0, 'Seconds':0};
      for (var key in t) {
        if (t.hasOwnProperty(key)) t[key] = date["get" +(utc ? "UTC" :"") + key]()
      }

      return this.month[t.Month]
        + " "
        + this.ordinal(t.Date)
        + ", "
        + t.FullYear
        + " @ "
        + this.clock12(t.Hours,t.Minutes);
      },
  month:
    [
      "January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
    ],
  ordinal:
    function(n) {
      return n+(
        [
          "th", "st", "nd", "rd"
        ][
          (( n % 100 / 10) | 0) === 1 ? 0 : n % 10 < 4 ? n % 10 : 0
        ]
      );
  },
  clock12:
    function(h24, m, s){
      h24%=24;
      var h12 = (h24 % 12) || 12;
      return h12 + ":" +
        (m < 10 ? "0" + m : m) +
        (isFinite(s) ? ":" + (s < 10 ? "0" + s : s ) : "") +
        (h24 < 12 ? "AM" : "PM");
      }
};

示例:

//Shows the date in the users timezone:
alert(ISODate.format("2007-09-21T14:15:34.058-07:00"));

//Show the date in UTC (Timezone Z, 00:00)
alert(ISODate.format("2007-09-21T14:15:34.058-07:00",true));

说明:

convert 将字符串作为输入,如果成功则返回日期对象,否则返回异常。该字符串必须采用以下格式之一:

  • YYYY-MM-DDTHH:MM:ss.sZ
  • YYYY-MM-DDTHH:MM:ss.sXaa:BB

其中:

  • YYYY是4位整数的年份
  • MM是2个整数的月份
  • DD是月份的日期,为2位整数
  • T是字符T或空格(\ x20)
  • hh是24小时格式的小时,为2位整数
  • mm是2位整数的分钟
  • ss.s是第二个,可以是2位整数,也可以是2位数的浮点,后跟句点后跟一个或多个数字。
  • Z是字符Z(表示时区Z,UTC + 00:00)
  • X是UTC时间偏移的加号(+)或减号( - )
  • aa是UTC的时间偏移小时,为2位整数
  • bb是ITC的时间偏移分钟,为2位整数

格式采用上述格式的字符串或日期对象,并返回格式为:

的字符串
  • M D,Y @ h:mm

其中    - M是月份的完整英文名称    - D是月份的日期,带有数字顺序后缀(1-2位数)    - Y是年份(1位或更多位)    - h是12小时格式的小时(1-2位数)    - m是分钟(2位数)

是一个包含月份名称的数组

序数是一个函数,它接受一个数字作为输入并返回带有英文序数后缀的数字。

clock12 是一种以24小时格式占用小时,分钟和秒的功能,并将其转换为美国12小时格式的字符串。 是可选的。

答案 1 :(得分:12)

试试http://www.datejs.com/。它是一个JavaScript Date Library,带有扩展的Date.parse方法和Date.parseExact方法,可以指定格式字符串。 请参阅DateJS APIDocumentation