Javascript 使用 datejs 解析 RFC3339 日期时间

标签 javascript datetime date datejs

我在使用来自 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

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);
}

这是一个 older answer对于不使用 date.js 时的这个问题

关于Javascript 使用 datejs 解析 RFC3339 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10531276/

相关文章:

java - 从 Date() 开始的上周日期

JavaScript 无法验证 HTML 中的表单

javascript - 在javascript中获取复选框值

java - 使用 Postman 在 header 中传递日期很热门吗?

R-绘制重叠的时间间隔

javascript - 保持午夜过后的营业时间

node.js mongodb 检索 ISODate 格式

javascript - 具有不同通用签名的继承

javascript - 关于分页符,如何让 Puppeteer PDF 生成与 HTML 文档完全匹配?

datetime - 如何将 xx 秒、分钟或小时添加到 DateTime 结构?