Julian Date
Join the Discussion
Related Resources
The date format where you use a five to seven digit number consisting of the day of the year and the year is commonly known as the Julian Date (not to be confused with Julian Day which is something else entirely). At first glance it appears that the methods provided for date objects in Javascript don't support Julian Dates and that a significant amount of code would be required in order to add processing into our Javascript in order to be able to handle dates in that format. Adding a method to the date object to allow the day of the year to be retrieved is in fact quite easy and so to obtain the Julian Date using that method would simply require the following code assuming that we already have a date object that contains the desired date (let's call that object "mydate").
The reason for using the String functions is to ensure that the values returned are concatenated together and not added together as would be the case if they were considered to be numbers. If we wanted to make sure that juldate was assigned a number rather than a string we could enclose everything after the = inside Number().
So that allows us to retrieve a Julian Date in just a few lines of code. How about if we want to take a julian date and create a date object from it?
As it turns out this is even easier to do as we don't need to add any extra methods to do it. To take a juldate and create a date object from it all we need is:
var day = Math.floor(julday/10000);
var mydate = new Date(year, 0, day);
The first two statements simply extract the day and year from the julian date while the third line creates a date object that is set to that date.
