使用JavaScript将iso8601日期时间字符串映射到儒略日 [英] Map iso8601 date time string to Julian Day using javascript
问题描述
任何人都可以通过以下格式的ISO-8601日期时间字符串获得紧凑/优美的地图:
Does anyone have a compact / elegant map from an ISO-8601 date time string of the following form:
2013-12-28T20:30:00-0700
到儒略时代.我希望找到一种避免使用外部库并且最小化正则表达式和字符串操作的解决方案.
To a Julian day. I'm hoping to find a solution that avoids an external library and has minimal regex and string manipulation.
推荐答案
这是一种方法.
您也可以将ISO字符串(也带有时区偏移)转换为现代JavaScript(ES5)中的JavaScript Date
对象.这适用于Node.js,Chrome和Firefox. Safari或IE尚不支持此功能.如果需要它在所有浏览器中都可以使用,则必须自己解析日期或使用类似 Moment.js 的库.
You can convert an ISO string—with a time zone offset too—to a JavaScript Date
object in modern JavaScript (ES5). This works in Node.js, Chrome and Firefox. It is not yet supported in Safari or IE. If you need it to work in all browsers, you have to parse the date yourself or use a library like Moment.js.
我针对美国海军天文台朱利安日期转换器测试了该算法日期范围内.
I tested this algorithm against the US Naval Observatory Julian Date Converter for a range of dates.
对于公历转换前的日期(1582年10月15日),它采用的是公历阳历,将与美国海军天文台显示的内容有所不同.
For dates prior to the Gregorian changeover (Oct. 15, 1582), this assumes the proleptic Gregorian calendar and will diverge from what the US Naval Observatory shows.
function julianDayNumber(d) {
var epoch = 2440587.500000; // Jan. 1, 1970 00:00:00 UTC
return d.getTime() / 86400000 + epoch;
}
样品用量:
console.log(julianDayNumber(new Date('2013-12-28T20:30:00-0700')));
// prints: 2456655.6458333335
这篇关于使用JavaScript将iso8601日期时间字符串映射到儒略日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!