Question

I am trying to convert Julian date string to DateTime but none of the solutions on the web seem to be working. I have a Julian date string 13324.

Julian Date: 13324

And I want to get the following DateTime

Date: 20-Nov-2013

Could you please direct me in the right direction. Thanks.

Was it helpful?

Solution

This is the simplest solution I can think of:

string julianDate = "13324";

int jDate = Convert.ToInt32(julianDate);
int day = jDate % 1000;
int year = (jDate - day) / 1000;
var date1 = new DateTime(year, 1, 1);
var result = date1.AddDays(day - 1);

(Note: this is all from memory; verify the syntax, etc.)

OTHER TIPS

Sorry for my bad english

Try this code if the one on top didn't work. It worked for me.

public DateTime ConvertJulianToDatetime(string julianDate)
{
    int dateint = int.Parse(julianDate);

    DateTime dinicio = new DateTime(1840, 12, 31);
    dinicio = dinicio.AddDays((double)dateint);
    return dinicio;
}

This code is more reliable I think (can anyone notice):

public static DateTime FromJD(double JD)
{
    return DateTime.FromOADate(JD - 2415018.5);
}

For MJD (modified julian day). Math.Floor or Math.Ceiling can be added as needed:

public static DateTime FromMJD(double MJD)
{     
    return DateTime.FromOADate((MJD - 2415018.5) + 2400000.5);
}

And one example for reverse translation:

public static double ToMJD(DateTime date)
{
    return (date.ToOADate() + 2415018.5) -2400000.5;
}

public static double ToJD(DateTime date)
{
    return date.ToOADate() + 2415018.5;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top