1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Julian Day

Adding to the Date Object

Join the Discussion

Questions? Comments?

Related Resources

Week of Year Day of Year

Adding our own methods to the objects that are built into Javascript is the easiest way that we can extend the functionality of those objects. Why create messy code that performs complex manipulations on those objects when you can add your own method to perform that complex calculation and consistently return the correct result from a simple call.

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:

Date.prototype.getJulian = function() {
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 today = new Date();
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.

See More About

©2017 About.com. All rights reserved.