« PreviousNext »

Converting Julian Date to .NET DateTime

18 February 2006

Want to convert a Julian Date to a .NET DateTime? Heres an easy (C#) way to do that

///<summary>
/// Will Convert a 7 digit Julian Date to a .NET DateTime.
/// 7 digit Julian dates are common in some legacy applications
/// such as JDE World, OneWorld, EnterpriseOne. You can easily make this
/// convert 8 digit julians (more common) just by removing the line that adds
/// 1900000 to the input date.
///</summary>
public static DateTime JulianToDateTime(int julianDate) 
{
   int RealJulian = julianDate + 1900000;
   int Year = Convert.ToInt32(RealJulian.ToString().Substring(0,4));
   int DoY = Convert.ToInt32(RealJulian.ToString().Substring(4));
   DateTime dtOut = new DateTime(Year,1,1);
   return dtOut.AddDays(DoY-1);
}

Posted in .NET Programming, ERP | Trackback | del.icio.us | Top Of Page

    2 Responses to “Converting Julian Date to .NET DateTime”

  1. zappster Says:

    There is an easier way. The datetime class has a method to process this. Use the following:

    To get a datetime from a Julian date:
    DateTime.FromOADate(julianDate);

    To get a Julian date from a datetime:
    DateTime.ToOADate(dateTimeValue);

  2. Tony Says:

    I disagree.
    The documentation on this says
    “Returns a DateTime equivalent to the specified OLE Automation Date”

    I went ahead and tested this anyway with the value of 2008001 - which should be 2008-01-01. That method returned a DateTime for 9/18/7397

    Unless you can figure out a fancy formula to get you from 2008001 to 39448 (the OLE date of 2008-01-01), I can’t see using FromOADate for converting Julian numeric dates.

Leave a Reply


You must be logged in to post a comment.