# Calculating Jday(Julian Day) in javascript

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 .

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

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

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

##### Julian Day

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

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.

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.

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.

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.

##### How to calculate it

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 and UTC share the same current time in practice, so for this, there should be no difference.

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.

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.

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.

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

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

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.

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.

##### Calculating it, again

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).

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

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 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';```

new Date().getTime()/86400000 + 2440587.5 will get the unix time stamp, convert it to days and add the JD of 1970-01-01, which is the epoch of the unix time stamp.

This is what astronomers call julian date. It is well defined. Since neither Unix time stamp nor JD take leap seconds into account that does not reduce the accuracy. Note that JD need not be in timezone UTC (but usually is). This answer gives you the JD in timezone UTC.

According to wikipedia:

```a = (14 - month) / 12
y = year + 4800 - a
m = month + 12a - 3
JDN = day + (153m + 2) / 5 + 365y + y/4 - y/100 + y/400 - 32045
```

If you're having a more specific problem with the implementation, provide those details in the question so we can help further.

NOTE : This is not correct because the "floor brackets" on Wiki were forgotten here.

The correct formulas are:

```a = Int((14 - Month) / 12)
y = Year + 4800 - a
m = Month + 12 * a - 3
JDN = Day + Int((153 * m + 2) / 5) + 365 * y + Int(y / 4) - Int(y / 100) + Int(y / 400) - 32045
```

Additionally, there is an npm package for this:

##### julian

Convert between Date object and Julian dates used in astronomy and history

```  var julian = require('julian');

var now = new Date();           // Let's say it's Thu, 21 Nov 2013 10:47:02 GMT
var jd = '';

console.log(jd = julian(now));  // -> '2456617.949335'
console.log(julian.toDate(jd)); // -> Timestamp above in local TZ
```

https://www.npmjs.com/package/julian

It seems that the final code given in the accepted answer is wrong. Check the "official" online calculator at US Naval Observarory website:

http://aa.usno.navy.mil/data/docs/JulianDate.php

If someone knows the correct answer to a answer time and calendar, it's USNO.

Whatever you do, DON'T USE getTimezoneOffset() on dates before a change of policy in the current Locale, it's completely broken in the past (it doesn't apply iana database rules). For example, if I enter (UTC date 1st of october 1995 at 00:00:00): var d=new Date(Date.UTC(1995, 9, 1, 0, 0, 0)); console.log(d.toLocaleString()); console.log(d.getTimezoneOffset()); in the javascript console in Chrome, it prints (I'm in France): 01/10/1995 at 01:00:00 <= this is winter time, +1:00 from UTC -120 <= BUT this is summer time offset (should be -60 for winter) Between 1973 and 1995 (included), DST (-120) terminated last Sunday of September, hence for 1st of October 1995, getTimezoneOffset() should return -60, not -120. Note that the formatted date is right (01:00:00 is the expected -60). Same result in Firefox, but in IE and Edge, it's worse, even the formatted date is wrong (01‎/‎10‎/‎1995‎ ‎02‎:‎00‎:‎00, matching the bad -120 result of getTimezoneOffset()). Whatever the browser (of these 4), getTimezoneOffset() uses the current rules rather than those of the considered date. Variation on the same problem when DST didn't applied in France (1946-1975), Chrome console: d=new Date(Date.UTC(1970, 6, 1, 0, 0, 0)); console.log(d.toLocaleString()); console.log(d.getTimezoneOffset()); displayed: ‎01‎/‎07‎/‎1970‎ ‎01:‎00‎:‎00 <= ok, no DST in june 1970, +1:00 -120 <= same problem, should be -60 here too And also, same thing in Firefox, worse in IE/Edge (01‎/‎07‎/‎1970‎ ‎02:‎00‎:‎00).