Javascript使用datejs来解析RFC3339 datetime [英] Javascript using datejs to parse RFC3339 datetime

查看:568
本文介绍了Javascript使用datejs来解析RFC3339 datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Calendar API的日期格式的datajs遇到问题。 datetime格式我认为是RFC3339,这是从日历api返回的示例datetime

  2012-01-05T08:45: 00Z 

这是从datejs文档这里

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

但这只是返回null。 >

我假设我有datejs正常工作

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

返回Fri May 11 2012 00:00:00 GMT + 0100(BST)

解决方案

SOLVED :使用Date.parseExact或此版本。 com / p / datejs / issues / detail?can = 2& start = 0& num = 100& q = rfc& colspec = ID%20Type%20Statu s%20Priority%20Milestone%20Owner%20Summary& groupby =& sort =& id = 147rel =noreferrer>错误报告



a href =http://jsfiddle.net/mplungjan/x2APB/ =noreferrer> DEMO使用date.js



DEMO使用date-en-US.js






使用第一个版本我得到

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

当我采取断言 testsuite

  // null 
console.log('Date.parse(1985-04-12T23:20:50Z)',
Date.parse '1985-04-12T23:20:50Z'));


//我以前的答案工作
console.log('Date.parse(1985-04-12T23:20:50Z.replace(...)) ',
Date.parse('1985-04-12T23:20:50Z'
.replace(/ \- / g,'\ /')
.replace(/ T | Z] / g,'')

);

//没有Z的测试套件
console.log('1985-04-12T23:20:50',
new Date(1985,3,12, 23,20,50).equals(Date.parse('1985-04-12T23:20:50')));

//但是这个失败的时候不在测试套件
尝试{
console.log('1985-04-12T23:20:50Z',
新日期(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时出现此问题


I'm having trouble using datajs with a date format from Google Calendar API. The datetime format I believe is RFC3339 and this is a sample datetime returned from the calendar api

2012-01-05T08:45:00Z

This is from the datejs documentation here

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

But this just returns null.

I'm assuming I have datejs working correctly as

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

returns Fri May 11 2012 00:00:00 GMT+0100 (BST)

解决方案

SOLVED: Use Date.parseExact OR this version according to this Bug report

DEMO using date.js

DEMO using date-en-US.js


Using the first version I get

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

when I take the assertions out of the 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);
}

Here is an older answer for this issue when not using date.js

这篇关于Javascript使用datejs来解析RFC3339 datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆