1

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,236
  • 14
  • 55
  • 90
Oxon
  • 4,651
  • 8
  • 37
  • 54
  • 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

4

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,078
  • 5
  • 34
  • 63
  • 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
0

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,241
  • 5
  • 11
  • 26
0

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
  • 18
  • 5