Calculating Jday(Julian Day) in JavaScript

Calculating Jday(Julian Day) in javascript

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

Calculating julian date with javascript

Based on the StackOverflow Question here, I have modified your JsFiddle

I have changed your implementation based on

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

(UNIX TIME / 86400000) + 2440587.5) === Julian Day;

and have updated your code to reflect as var JD = time/86400000 + 2440587.5; To get the same result as the online calculator I had to use the UTC time you used with the online examples.

Convert a Date to a Julian javascript

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

var valDate = input1[0];
var dt = new Date(valDate);
var julian_dt = dt.getJulian();

output1 = julian_dt;

i was able to use the code above.

Thanks

convert javascript date to julian date

You do not need Julian Date to compare.

You need Date.parse() function or compare by comparison operators. It will return the milliseconds that have passed since 01/01/1970 00:00

Somehow like this:

if(Date.parse(fromDate) < Date.parse(toDate){
//start is less than End
}else{
//end is less than start
}

Here is a Fiddle

Java - Calculation of the julian day number to gregorian date and time

Edit: Java has a rough support for Julian day number. It counts the days correctly. It doesn’t begin a new day at noon as the Julian day defines, and it doesn’t support fraction of day. We can still use the support and make the proper adjustment ourselves.

As far as I can see the following methods make a consistent conversion forth and back and agree with the Julian day number that you got from toJulian() in JavaScript.

private static final double dayMs = Duration.ofDays(1).toMillis();

public static double toJulian(LocalDateTime date) {
LocalTime timeOfDay = date.toLocalTime();
double result = date.getLong(JulianFields.JULIAN_DAY);
result += timeOfDay.get(ChronoField.MILLI_OF_DAY) / dayMs - 0.5;

return result;
}

public static LocalDateTime fromJulian(double jdn) {
LocalDate date = LocalDate.EPOCH.with(JulianFields.JULIAN_DAY, (long) jdn);
LocalDateTime result = date.atStartOfDay()
.plusHours(12)
.plus((long) (jdn % 1 * dayMs), ChronoUnit.MILLIS);

return result;
}

Trying them out:

    LocalDateTime dateTime = LocalDateTime.of(2020, Month.DECEMBER, 26, 0, 0);
double jdn = toJulian(dateTime);
System.out.println(jdn);

LocalDateTime convertedBack = fromJulian(jdn);
System.out.println(convertedBack);
System.out.println("Does it differ? " + Duration.between(dateTime, convertedBack));

Output:

2459209.5
2020-12-26T00:00
Does it differ? PT0S

The last line contains PT0S, a period of time of 0 seconds, so the result does not differ from the starting point.

Documentation link: JulianFields.JULIAN_DAY.

How to loop through the remaining days of the year and look for certain dates in javascript?

What about something like this? (see the jsfiddle):

var birthdays = [
{
name: 'Mark',
date: 'October 20'
},
{
name: 'Robbin',
date: 'December 5'
}
];

var upcoming = birthdays.filter(function(person) {
var date = new Date(person.date + ' 2015');

// returns a boolean "true" or "false"
return date > new Date();
});

console.log(upcoming);

It simply filters the current birthdays into a new array, upcoming, if the birthday is greater than today's date. No need to overcomplicate it.

You could make it more semantic and extensible if you so desire:

var upcoming = birthdays.filter(afterToday);

function afterToday(person) {
return new Date(person.date + ' 2015') > new Date();
}

One of the key paradigms of programming is that simpler is better. Many algorithms can do the same thing (end up with the same results), but usually the simpler algorithm is better, all else being equal.

In other words:

  1. The algorithm can loop through every single date remaining in the year, or

  2. The algorithm can simply check if the date is greater than today

I think the second is simpler. Also, less lines of code, and more readable.

Extract day of year and Julian day from a string date

First, you can convert it to a datetime.datetime object like this:

>>> import datetime
>>> fmt = '%Y.%m.%d'
>>> s = '2012.11.07'
>>> dt = datetime.datetime.strptime(s, fmt)
>>> dt
datetime.datetime(2012, 11, 7, 0, 0)

Then you can use the methods on datetime to get what you want… except that datetime doesn't have the function you want directly, so you need to convert to a time tuple

>>> tt = dt.timetuple()
>>> tt.tm_yday
312

The term "Julian day" has a few different meanings. If you're looking for 2012312, you have to do that indirectly, e.g., one of the following.

>>> int('%d%03d' % (tt.tm_year, tt.tm_yday))
2012312
>>> tt.tm_year * 1000 + tt.tm_yday
2012312

If you're looking for a different meaning, you should be able to figure it out from here. For example, if you want the "days since 1 Jan 4713 BC" meaning, and you have a formula that requires Gregorian year and day in year, you've got those two values above to plug in. (If you have a formula that takes Gregorian year, month, and day, you don't even need the timetuple step.) If you can't work out where to go from there, ask for further details.

If you don't have a formula—and maybe even if you already do—your best bet is probably to look around PyPI and ActiveState for pre-existing modules. For example, a quick search turned up something called jdcal. I'd never seen it before, but a quick pip install jdcal and a brief skim of the readme, and I was able to do this:

>>> sum(jdcal.gcal2jd(dt.year, dt.month, dt.day))
2456238.5

That's the same result that the USN Julian date converter gave me.

If you want integral Julian day, instead of fractional Julian date, you have to decide which direction you want to round—toward 0, toward negative infinity, rounding noon up to the next day, rounding noon toward even days, etc. (Note that Julian date is defined as starting since noon on 1 Jan 4713BC, so half of 7 Nov 2012 is 2456238, the other half is 2456239, and only you know which one of those you want…) For example, to round toward 0:

>>> int(sum(jdcal.gcal2jd(dt.year, dt.month, dt.day)))
2456238


Related Topics



Leave a reply



Submit