Javascript使用datejs来解析RFC3339 datetime

编程入门 行业动态 更新时间:2024-10-27 16:34:43
本文介绍了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 =jsfiddle/mplungjan/x2APB/ =noreferrer> DEMO使用date.js

DEMO使用date-en-US.js

使用第一个版本我得到

null http:// datejs .googlecode / 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 datejs.googlecode/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

本文发布于:2023-11-03 16:54:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:datejs   Javascript   datetime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!