3

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.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Oxon
  • 4,901
  • 8
  • 40
  • 54
  • 1
    Julian date starts at 4713 BC while the first `DateTime` representable is 0-0-0001. I'm curious to see how this will be solved. – Jeroen Vannevel Nov 29 '13 at 19:23

3 Answers3

6

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.)

Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • I get the correct result if I replace the 4th line with the following. `int year = (jDate - day + 2000000) / 1000;` – Oxon Dec 02 '13 at 11:15
  • You will get a different result if you convert `13324` on this page http://aa.usno.navy.mil/data/docs/JulianDate.php and this page http://www.longpelaexpertise.com.au/toolsJulian.php Which one is correct? I tried converting the formula on this page to C# but it didnt give me a correct DateTime equivalent: http://webcache.googleusercontent.com/search?q=cache:UKCRN0ijcZcJ:quasar.as.utexas.edu/BillInfo/JulianDatesG.html+&cd=4&hl=en&ct=clnk&gl=uk – Oxon Dec 02 '13 at 11:19
  • 1
    As Jeroen Vannevel points out, the true Julian Date starts at 4713 BC. That's the value that those websites show. What you're interested in is commonly referred to as a Julian date, but is more properly called the Ordinal Date: http://en.wikipedia.org/wiki/Ordinal_date – Ann L. Dec 02 '13 at 13:52
1

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;
}
fcdt
  • 2,371
  • 5
  • 14
  • 26
1

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;
}
devalurum
  • 58
  • 7
  • Thanks in summary; A typical conversion uses 2415018.5 = 1899-12-30, Modified date uses: 2400000.5 = 1858-11-17, Truncated date uses: 2440000.5 = 1968-05-24 – nimblebit Nov 03 '22 at 13:57