我在使用来自 Google Calendar API 的日期格式的 datajs 时遇到问题。我认为日期时间格式是 RFC3339,这是从日历 api 返回的示例日期时间
2012-01-05T08:45:00Z
这是来自 datejs 文档 here
Date.parse('1985-04-12T23:20:50Z') // RFC 3339 Formats
但这只会返回 null。
我假设我有 datejs 正常工作
Date.today().next().friday()
2012 年 5 月 11 日星期五 00:00:00 GMT+0100(英国夏令时)返回
最佳答案
已解决:使用 Date.parseExact 或 this version根据这个Bug report
使用我得到的第一个版本
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);
}
这是一个 older answer对于不使用 date.js 时的这个问题
关于Javascript 使用 datejs 解析 RFC3339 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10531276/