Javascript使用datejs来解析RFC3339 datetime

时间:2012-05-10 09:38:12

标签: javascript datetime date datejs

我在使用带有Google Calendar API日期格式的dataj时遇到问题。我认为日期时间格式是RFC3339,这是从日历api返回的示例日期时间

2012-01-05T08:45:00Z

这来自datejs文档here

Date.parse('1985-04-12T23:20:50Z')          // RFC 3339 Formats

但这只是返回null。

我假设我的日期工作正常

Date.today().next().friday() 

返回2012年5月11日星期五00:00:00 GMT + 0100(BST)

1 个答案:

答案 0 :(得分:6)

已解决:根据此this version使用Date.parseExact或Bug report

DEMO using date.js

DEMO using date-en-US.js


使用我得到的第一个版本

null
http://datejs.googlecode.com/files/date.js
Line 13

当我从testsuite

中取出断言时
// null
console.log('Date.parse("1985-04-12T23:20:50Z")',
  Date.parse('1985-04-12T23:20:50Z'));


// my previous answer works
console.log('Date.parse("1985-04-12T23:20:50Z".replace(...))',
     Date.parse('1985-04-12T23:20:50Z'
           .replace(/\-/g,'\/')
           .replace(/[T|Z]/g,' ')
     )
  );

// the test suite without the Z works
console.log('1985-04-12T23:20:50',
  new Date(1985,3,12,23,20,50).equals( Date.parse('1985-04-12T23:20:50')));

// but this one fails when not in the test suite    
try {
  console.log('1985-04-12T23:20:50Z',
    new Date(1985,3,12,23,20,50).equals( Date.parse('1985-04-12T23:20:50Z')));
}
catch(e) {
     console.log('1985-04-12T23:20:50Z',e.message);
}

不使用date.js

时,此问题为older answer