Convert DateTime to Julian Date in C# (ToOADate Safe?)

I need to convert from a standard Gregorian date to a Julian day number.

I've seen nothing documented in C# to do this directly, but I have found many posts (while Googling) suggesting the use of ToOADate.

The documentation on ToOADate does not suggest this as a valid conversion method for Julian dates.

Can anyone clarify if this function will perform conversion accurately, or perhaps a more appropriate method to convert DateTime to a Julian formatted string.


This provides me with the expected number when validated against Wikipedia's Julian Day page

public static long ConvertToJulian(DateTime Date)
{
    int Month = Date.Month;
    int Day = Date.Day;
    int Year = Date.Year;

    if (Month < 3)
    {
        Month = Month + 12;
        Year = Year - 1;
    }
    long JulianDay = Day + (153 * Month - 457) / 5 + 365 * Year + (Year / 4) - (Year / 100) + (Year / 400) + 1721119;
    return JulianDay;
}

However, this is without an understanding of the magic numbers being used.

Thanks


References:

29
задан cweston 9 March 2011 в 19:07
поделиться