在javascript中计算Jday(Julian Day) [英] Calculating Jday(Julian Day) in javascript

查看:120
本文介绍了在javascript中计算Jday(Julian Day)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求在javascript中计算 jday ,进行客户端验证,任何人都可以帮我计算 JDAY 在javascript或脚本中将给定的JDAY更改为实际日期,反之亦然。

I have requirement to calculate jday in javascript , for doing client side validation , Can any one help me how to calculate JDAY in javascript or script to change given JDAY to actual date or vice versa .

要知道什么是JDay,我找到了以下网站,

To know what is JDay ,I found the following site ,

http://www.pauahtun.org/Software/jday.1.html

我也引用以下网站进行JAVA中提到的计算

Am also refering the below site for calculation which is mentioned in JAVA

http://www.rgagnon.com/javadetails/java-0506.html

提前谢谢

推荐答案

朱利安日



朱利安<强>日是自7980年周期开始以来经过的天数。

Julian Day

The Julian Day is the number of elapsed days since the beginning of a cycle of 7980 years.

由Joseph Scaliger于1583年发明,该系统的目的是简化计算整数(整数)差异在一个日历日期和另一个日历日期之间。

Invented in 1583 by Joseph Scaliger, the purpose of the system is to make it easy to compute an integer (whole number) difference between one calendar date and another calendar date.

7980年周期是通过结合几个传统的时间周期(太阳能,月球和特定的罗马税收周期)得出的,其中7980是一个共同的倍数。

The 7980 year cycle was derived by combining several traditional time cycles (solar, lunar, and a particular Roman tax cycle) for which 7980 was a common multiple.

第一个朱利安周期的起点始于公元前4713年1月1日。格林威治标准时间中午,将于格林威治标准时间中午3268年1月22日结束,整天后正好结束7980。

The starting point for the first Julian cycle began on January 1, 4713 B.C. at noon GMT, and will end on January 22, 3268 at noon GMT, exactly 7980 whole days later.

例如,2016年1月1日的朱利安日数是2,457,389,这是自公元前4713年1月1日以来的天数在那一天。

As an example, the Julian day number for January 1, 2016 was 2,457,389, which is the number of days since January 1, 4713 B.C. at that day.

我们知道Unix时间是自秒以来的秒数1970年1月1日00:00:00,不计算闰秒,也称为Epoch,我们可以使用一些数学来计算我们已经拥有Unix时间的Julian日。

As we know that Unix time is the number of seconds since 00:00:00 UTC, January 1, 1970, not counting leap seconds, and also called Epoch, we can use some math to calculate the Julian Day when we already have the Unix time.

GMT和UTC在实践中分享相同的当前时间,所以为此,应该没有区别。

GMT and UTC share the same current time in practice, so for this, there should be no difference.

首先,我们需要知道从Julian周期开始到Unix时间戳开始的天数。

换句话说,从公元前4713年1月1日开始的天数格林威治标准时间12:00:00,直到1970年1月1日00:00:00 UTC。

To start with, we need to know the number of days from when the Julian cycle began, until Unix timestamps began.
In other words, the number of days from January 1, 4713 B.C. at 12:00:00 GMT, until January 1, 1970 at 00:00:00 UTC.

有了这个设定的天数,从未改变,我们可以添加从1970年1月1日到今天的天数,无论如何Javascript都会返回,以获得朱利安日。

Having this set number of days, that never change, we can just add the number of days from January 1, 1970 until today, which is what Javascript returns anyway, to get the Julian Day.

这些年没有加起来,而只是通过搜索网络,它告诉我们公元前4713年之间的天数差异公元1970年是2440588天,​​因为朱利安周期从中午开始,而不是午夜,我们必须减去半天,使其为2440587.5天。

Without adding up all those years, but simply by searching the web, it tells us that the difference in days between the year 4713 B.C. and 1970 A.D. is 2440588 days, and because the Julian Cycle began at noon, not at midnight, we have to subtract exactly half a day, making it 2440587.5 days.

所以我们现在拥有的是 2440587.5天+ UNIX TIME天数=== Julian Day

So what we have now is 2440587.5 days + UNIX TIME in days === Julian Day

通过一些简单的数学运算我们可以看出一天是86,400秒,使用Javascript时Unix时间戳是毫秒,所以 UNIX TIME / 86400000 会得到我们自星期四以来的天数, 1970年1月1日,直到今天。

With some simple math we can figure out that a day is 86,400 seconds long, and the Unix timestamp is in milliseconds when using Javascript, so UNIX TIME / 86400000 would get us the number of days since Thursday, 1 January 1970, until today.

现在只是一天,我们想要整天,而不是分数,并且可以将它整理到收盘整天,做类似

Now for just the day, we wanted the whole number of days, and not the fractional, and can just round it down to the closes whole day, doing something like

Math.floor((UNIX TIME / 86400000) + 2440587.5);



Julian Date



有时在编程中, 朱利安日期已经表示自该年开始以来的天数,例如2016年6月1日将是该年的152天等。

Julian Date

Sometimes in programming, a "Julian Date" has come to mean the number of days since the year started, for instance June 1, 2016 would be 152 days into that year etc.

正确使用朱利安日期是朱利安日,时间戳添加为当天的小数部分。

The correct use of "Julian Date" is a Julian Day with a timestamp added as a fractional part of the day.

以此答案的顶部为例,其中1月1日,2016年是朱利安日2,457,389,我们可以增加一个时间。

朱利安日从中午开始,没有添加小数时间,所以在午夜它将是 2457389.5 在18:00或中午6点后,它将 2457389.25 ,加上半天,一天中的四分之一等等。

Taking the example at the top of this answer, where January 1, 2016 was the Julian Day 2,457,389 , we can add a time to that.
The Julian Day starts at noon, with no fractional time added, and so at midnight it would be 2457389.5 and at 18:00, or six hours after noon, it would be 2457389.25, adding "half a day", "quarter of a day" etc.

这意味着0.1 Julian日期与24小时相同除以10 ,或 24/10 === 2.4小时,换句话说,Julian Day时间戳是小数的小数(十分之一)一天等)

This means 0.1 Julian Date is the same as 24 hours divided by 10, or 24 / 10 === 2.4 hours, or in other words, Julian Day timestamps are fractional with decimals (one tenth of a day etc).

让我们看看一些Javascript函数,首先是 Date 构造函数。

Lets look at some Javascript functions, firstly the Date constructor.

Javascript只能访问运行它的计算机上的本地时间,因此当我们执行 new Date()时,它不会成功创建UTC日期,即使UNIX时间是UTC, new Date 也会为您提供从纪元到计算机当地时间的秒数,并且不会考虑您的时区。

Javascript only has access to the local time on the computer it runs on, so when we do new Date() it does not neccessarely create an UTC date, even if UNIX time is in UTC, new Date gives you the number of seconds from epoch until whatever local time your computer has, and does not take your timezone into consideration.

Javascript确实有 Date.UTC ,它会以UTC格式返回日期,让我们检查差异,这个当然会根据您设置本地系统的时区而有所不同。

Javascript does however have Date.UTC, which would return the date in UTC format, lets check the difference, and this will of course differ according to the timezone you've set the local system to.

var regular_date = new Date(2016, 1, 1, 0, 0, 0);
var UTC_date     = Date.UTC(2016, 1, 1, 0, 0, 0);
var difference   = UTC_date - regular_date;

document.body.innerHTML = 'The difference between your local time and UTC is ' +(difference/1000)+ ' seconds';

请记住本章开头的部分,大约0.1 Julian日期与24小时相同,除以10或 24/10 === 2.4小时,好吧,2.4小时是144分钟,现在让我们快速查看Javascripts getTimezoneOffset()方法,文档说

Remember the part at the begin of this chapter, about 0.1 Julian Date being the same as 24 hours divided by 10, or 24 / 10 === 2.4 hours, well, 2.4 hours is 144 minutes, and now lets look quickly at Javascripts getTimezoneOffset() method, the docs say


getTimezoneOffset()方法返回UTC的时区偏移量,以分钟为单位的
locale。

The getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale.

因此,它会在分钟中返回系统时区的偏移量,这与大多数javascript一样有趣处理日期的方法返回毫秒。

So, it returns the offset for the systems timezone in minutes, that's interesting as most javascript methods that deal with dates returns milliseconds.

我们知道一天中的1/10是144分钟,所以10/10或一整天将是1440分钟,所以我们可以使用一些数学抵消以分钟为单位的本地系统时区,并将其除以一天中的分钟数,以获得正确的小数值

We know that a 1/10 of a day is 144 minutes, so 10/10, or a whole day, would be 1440 minutes, so we could use some math to counteract the local systems timezone, given in minutes, and divide it by the number of minutes in a day, to get the correct fractional value

现在我们已经

2440587.5 days + UNIX TIME in days === Julian Day

我们知道Javascripts Date构造函数并没有真正使用UTC作为当前日期,而是系统时间,因此我们必须

and we know Javascripts Date constructor doesn't really use UTC for the current date, but the system time, so we have to have

TIMEZONEOFFSET / 1440

将他们加在一起我们会得到

joining them together we would get

(JAVASCRIPT TIME / 86400000) - (TIMEZONEOFFSET / 1440) + 2440587.5
//  ^^ days since epoch ^^      ^^ subtract offset ^^    ^^days from 4713 B.C. to 1970 A.D.

将其转换为javascript将是

Translating that to javascript would be

var date = new Date();     // a new date
var time = date.getTime(); // the timestamp, not neccessarely using UTC as current time

var julian_day = (time / 86400000) - (date.getTimezoneOffset()/1440) + 2440587.5);

现在我们应该用它来获得Julian Day ,采取措施去除时区偏移,当然没有朱利安日期的小数时间部分。

我们这样做是通过简化舍入到最接近的整数

Now this is what we should use to get the Julian Day as well, taking measures to remove the timezone offset, and of course without the fractional time part of the Julian Date.
We would do this by simpy rounding it down to the closest whole integer

var julian_date = Math.floor((time / 86400000) - (date.getTimezoneOffset()/1440) + 2440587.5));

现在是时候回答这个问题了,在我做了这么长时间的编辑来解释原因之前在评论字段中投诉后,这是正确的方法。

And it's time for my original answer to this question, before I made this extremely long edit to explain why this is the correct approach, after complaints in the comment field.

Date.prototype.getJulian = function() {
  return Math.floor((this / 86400000) - (this.getTimezoneOffset() / 1440) + 2440587.5);
}

var today = new Date(); //set any date
var julian = today.getJulian(); //get Julian counterpart

console.log(julian)

.as-console-wrapper {top:0}

并且相同 with the fracional part

And the same with the fracional part

Date.prototype.getJulian = function() {
  return (this / 86400000) - (this.getTimezoneOffset() / 1440) + 2440587.5;
}

var today = new Date(); //set any date
var julian = today.getJulian(); //get Julian counterpart

console.log(julian)

.as-console-wrapper { top: 0 }

并完成,一个显示原因的例子

And to finish of, an example showing why

new Date().getTime()/86400000 + 2440587.5

不起作用,至少在系统时间设置为带偏移的时区时不起作用,即GMT以外的任何其他内容

doesn't work, at least not if your system time is set to a timezone with an offset, i.e. anything other than GMT

// the correct approach
Date.prototype.getJulian = function() {
    return (this / 86400000) - (this.getTimezoneOffset() / 1440) + 2440587.5;
}

// the simple approach, that does not take the timezone into consideration
Date.prototype.notReallyJulian = function() {
  return this.getTime()/86400000 + 2440587.5;
}
// --------------

// remember how 18:00 should return a fractional 0.25 etc
var date = new Date(2016, 0,    1,   18,   0,   0,    0); 
//                   ^    ^     ^     ^    ^    ^     ^
//                 year  month date  hour min  sec  milli
                   
var julian = date.getJulian();       //get Julian date
var maybe  = date.notReallyJulian(); // not so much

console.log(julian); // always returns 2457389.25
console.log(maybe);  // returns different fractions, depending on timezone offset

.as-console-wrapper { top: 0 }

这篇关于在javascript中计算Jday(Julian Day)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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