2

Does anyone have a compact / elegant map from an ISO-8601 date time string of the following form:

2013-12-28T20:30:00-0700

To a Julian day. I'm hoping to find a solution that avoids an external library and has minimal regex and string manipulation.

Ben Pearce
  • 6,884
  • 18
  • 70
  • 127
  • 1
    Do you mean real [Julian Day](http://en.wikipedia.org/wiki/Julian_day) or maybe [Ordinal Date](http://en.wikipedia.org/wiki/Ordinal_date)? – Teemu Dec 18 '13 at 20:06
  • possible duplicate of [Calculating Jday(Julian Day) in javascript](http://stackoverflow.com/questions/11759992/calculating-jdayjulian-day-in-javascript) – Matt Johnson-Pint Dec 19 '13 at 03:08

1 Answers1

3

Here’s one way to do it.

You can convert an ISO string—with a time zone offset too—to a JavaScript Date object in modern JavaScript (ES5). This works in Node.js, Chrome and Firefox. It is not yet supported in Safari or IE. If you need it to work in all browsers, you have to parse the date yourself or use a library like Moment.js.

I tested this algorithm against the US Naval Observatory Julian Date Converter for a range of dates.

For dates prior to the Gregorian changeover (Oct. 15, 1582), this assumes the proleptic Gregorian calendar and will diverge from what the US Naval Observatory shows.

function julianDayNumber(d) {
  var epoch = 2440587.500000;                   // Jan. 1, 1970 00:00:00 UTC
  return d.getTime() / 86400000 + epoch;
}

Sample usage:

console.log(julianDayNumber(new Date('2013-12-28T20:30:00-0700')));
// prints: 2456655.6458333335
Nate
  • 18,752
  • 8
  • 48
  • 54
  • Sorry for the downvote, but moment.js doesn't have any function for calculating a Julian day number. – Matt Johnson-Pint Dec 19 '13 at 03:06
  • Fair enough. I have a question though. By Julian day number, are you (and OP) looking for the number of days elapsed since January 1, 4713 BC? What field does this apply to? My guess would be astronomy? – Nate Dec 19 '13 at 03:24
  • The Julian day is related to the Julian Calendar. You can read about it [here](http://en.wikipedia.org/wiki/Julian_day). Accounting for differences between Julian and Gregorian calendars, it could be calculated by counting the number of whole days since 24 November 4714 BC by the *prolepetic* Gregorian calendar (proleptic means, assume it was always in effect even though it wasn't). Yes, it's used in Astronomy, among other fields. – Matt Johnson-Pint Dec 19 '13 at 03:31
  • You could probably *use* moment.js to parse the ISO date/time and calculate the Julian date from there. Hint: Jan 1 2000 has a Julian date of 2451545. I'll be happy to upvote if you care to edit to provide such a function. – Matt Johnson-Pint Dec 19 '13 at 03:34
  • Thanks, @MattJohnson. Now that I have a better idea what he’s looking for, it wasn’t hard to write the algorithm. Of course I still don’t know the OP’s full requirements: does he need pre-1582 dates? server side or client side? Etc. – Nate Dec 19 '13 at 04:33
  • Technically speaking, the ISO 8601 spec is also based on the proleptic Gregorian calendar, and so are unix timestamps and js `Date` values, so there's not much point in returning NaN for dates before 1582. Other than that, looks reasonable. Except not sure why you put half a day on the epoch. – Matt Johnson-Pint Dec 19 '13 at 05:04
  • 1
    References I found show midnight as being 0.5, like this one: http://aa.usno.navy.mil/data/docs/JulianDate.php. – Nate Dec 19 '13 at 05:16
  • Interesting. I had assumed that midnight was zero. good find! – Matt Johnson-Pint Dec 19 '13 at 15:30