I looked online and couldn't see an actual formula or anything, so I figured I'd ask here.
If I had an MJD like the following: 59145.6678
How would I convert that to a month, day, and year with the hour and minutes?
Thanks in advance!
Astronomy Stack Exchange is a question and answer site for astronomers and astrophysicists. It only takes a minute to sign up.
Sign up to join this communityI looked online and couldn't see an actual formula or anything, so I figured I'd ask here.
If I had an MJD like the following: 59145.6678
How would I convert that to a month, day, and year with the hour and minutes?
Thanks in advance!
A modified Julian Date is just a Julian Date minus 2,400,000.5. So you could just add 2,400,000.5 to the MJD, and use existing Julian Date routines to do the conversion. Here are some routines which break a JD into Month, Day, Year and vice versa.
But, if you're using any modern language which has built in date routines, I highly recommend you use those instead to avoid the many special cases when dealing with dates.
Most languages will accept the time in seconds since Jan 1, 1970 ("Unix Time") as an input to date routines, and the functions below will convert a MJD to/from Unix Time in milliseconds:
function ModifiedJulianDateFromUnixTime(t){
return (t / 86400000) + 40587;
}
function UnixTimeFromModifiedJulianDate(jd){
return (jd-40587)*86400000;
}
The above functions are in Javascript, but it is trivial to convert to nearly any other language. Some languages accept seconds rather than milliseconds, so divide by 1000 for those.
For, example, in JavaScript, the code below will get all of the components.
date = new Date(UnixTimeFromModifiedJulianDate(59145.6678));
day=date.getDate();
year=date.getYear()+1900;
month=date.getMonth()+1;
hour=date.getHours();
minutes=date.getMinutes();
seconds=date.getSeconds();
milli=date.getMilliseconds();
console.log(year,month,day,hour,minutes,seconds,milli);
Returns:
2020 10 23 12 1 37 920
You must be careful about any dates prior to Oct 15, 1582 (the start of the Gregorian Calendar). What answer is correct depends a lot on what your intentions are. The routines in the link above assume anything before the Gregorian Calendar are in the Julian Calendar, which matches what JPL Horizons does.