首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用datejs解析RFC3339 datetime的Javascript

使用datejs解析RFC3339 datetime的Javascript
EN

Stack Overflow用户
提问于 2012-05-10 17:38:12
回答 1查看 2.2K关注 0票数 4

我在使用Google Calendar API中的日期格式的datajs时遇到了问题。我相信日期时间格式是RFC3339,这是从日历api返回的示例日期时间

代码语言:javascript
复制
2012-01-05T08:45:00Z

这来自datejs文档here

代码语言:javascript
复制
Date.parse('1985-04-12T23:20:50Z')          // RFC 3339 Formats

但这只会返回null。

我假设我的datejs工作正常,因为

代码语言:javascript
复制
Date.today().next().friday() 

返回2012年5月11日星期五00:00:00 GMT+0100 (英国夏令时)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-10 18:01:55

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

DEMO using date.js

DEMO using date-en-US.js

使用我得到的第一个版本

代码语言:javascript
复制
null
http://datejs.googlecode.com/files/date.js
Line 13

当我将断言从testsuite中删除时

代码语言:javascript
复制
// 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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10531276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档