I have data in MJD format that needs to be modified to decimal year format before I can work with it. I found the following link to convert from standard dates to MJD, but not the reverse (e.g. May 24, 2005 to 53514): http://www.mathworks.com/access/helpdesk/help/toolbox/aerotbx/ug/mjuliandate.html
I was thinking of writing the code myself, that is until I saw how complicated it can be. Now I'm curious to see if there is a function or another such easy way to proceed. Or perhaps I'm making it slightly harder than it needs to be?
Any ideas on how to do this and account for leap years/days/hours?
I appreciate any help that can be provided.
This javascript code is from the following web site
http://www.csgnetwork.com/julianmodifdateconv.html
It should be a simple matter to convert to Matlab, just remove the type declarations,
replace Math.floor() with floor(), and change the function declaration to return the Month, Day, Year as required.
Hth,
Darren
// Convert mjd to/from Month, Day, Year,
function MJDtoYMD (mjd_in)
{
var year;
var month;
var day;
var hour;
var jd;
var jdi;
var jdf
var l;
var n;
// Julian day
jd = Math.floor (mjd_in) + 2400000.5;
// Integer Julian day
jdi = Math.floor (jd);
// Fractional part of day
jdf = jd - jdi + 0.5;
// Really the next calendar day?
if (jdf >= 1.0) {
jdf = jdf - 1.0;
jdi = jdi + 1;
}
hour = jdf * 24.0;
l = jdi + 68569;
n = Math.floor (4 * l / 146097);
l = Math.floor (l) - Math.floor ((146097 * n + 3) / 4);
year = Math.floor (4000 * (l + 1) / 1461001);
l = l - (Math.floor (1461 * year / 4)) + 31;
month = Math.floor (80 * l / 2447);
day = l - Math.floor (2447 * month / 80);
l = Math.floor (month / 11);
month = Math.floor (month + 2 - 12 * l);
year = Math.floor (100 * (n - 49) + year + l);
if (month < 10)
month = "0" + month;
if (day < 10)
day = "0" + day;
//year = year - 1900;
return (new Array (year, month, day));
}
Why not use the built-in Matlab functions?
As I understand it, MJD of 53005 is 20040101, so:
datenum('20040101','yyyymmdd')-53005
ans =
678942
is the conversion from Matlab days to MJD.
So, for 20100101:
MJD=datenum('20100101','yyyymmdd')-678942
and MJD=53000 is:
datestr(678942+53000)
[year,month,day,hour,minute,second] = datevec(MJD + 678942);
where MJD: modified Julian date
Example:
To convert 55197.000 to a Date Vector
MJD = 55197.000;
[year,month,day,hour,minute,second] = datevec(MJD + 678942);
or you can run the command
datestr(MJD+ 678942,0)
ans = 01-Jan-2010 00:00:00
Ossama F.