Adding to the Date Object
Join the Discussion
Here we are going to look at how we can easily add processing to our date objects to allow us to access the Julian Day associated with any particular date. In fact this doesn't require complex processing, it just requires that we use three numbers in the calculation at least one of which is not easy to remember and so by creating a method to return the Julian Day we can just set up the code in one place and not have to worry about remebering the numbers that we would otherwise need to use to calculate it. Here's the code to add a getJulian() method to all dates:
return Math.floor((this / 86400000) -
(this.getTimezoneOffset()/1440) + 2440587.5);
So now that we have added the code to add the method, let's take a quick look at how to use it. The Julian Day right now is . We can obtain that value by using the following code.
var julianDay = today.getJulian();
To get the Julian day for any other date you just need to have a date object defined with the date that you want and then call the getJulian() method for that object.
If you want your Julian Day method to return fractions of a day rather than just the actual day number itself then simply remove the Math.floor() from around the calculation.