Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1197
  • Last Modified:

Julian Date Convertion (2454115.05486)

Hi all,

I am trying to convert this julian date 2454115.05486 into a standard date and time.
Please can someone show an exaple in either VB.net or C#

Cheers
Zeus
0
Zeus2009
Asked:
Zeus2009
  • 6
  • 4
  • 2
  • +2
1 Solution
 
Anil GolamariCommented:
using System;
 
namespace DateFromDouble
{
    class Program
    {
        static void Main(string[] args)
        {
                Console.WriteLine(DateTime.FromOADate(39820.0));
        }
    }
}
0
 
Anil GolamariCommented:
  ///<summary>
2    /// Will Convert a 8 digit Julian Date to a .NET DateTime.
3    /// 8 digit Julian dates are common in some legacy applications
4    /// such as JDE World, OneWorld, EnterpriseOne. You can easily make this
5    /// convert 7 digit julians just by uncommenting the line that adds
6    /// 1900000 to the input date and commenting out the line above it.
7    ///</summary>
8    public static DateTime JulianToDateTime(int julianDate)
9    {
10        int RealJulian = julianDate;
11        //int RealJulian = julianDate + 1900000;
12        int Year = Convert.ToInt32(RealJulian.ToString().Substring(0, 4));
13        int DoY = Convert.ToInt32(RealJulian.ToString().Substring(4));
14        DateTime dtOut = new DateTime(Year, 1, 1);
15        return dtOut.AddDays(DoY - 1);
16    }

for reference: http://www.codekeep.net/snippets/512c98b3-15ee-4be8-beb3-ce1020308861.aspx
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
ladarlingCommented:
I have some experience with the Julian format, but I'm not reading this right or something... what portion of the date contains the month/day ordinal in your example?
0
 
John (Yiannis) ToutountzoglouCommented:
Try this Function Found in
http://www.kconnolly.net/Post.aspx?Title=VB.NET:+Converting+Julian+Date+to+DateTime
Public Shared Function ReverseJulian(ByVal JulianDate As String) As DateTime 'input format: yyyyddd (i.e. "2008180")
 'Initial validation
 JulianDate = JulianDate.Trim()
 If JulianDate.Length <> 7 Then Return Nothing

 'Date conversion
 Dim dtOut As DateTime = DateTime.Parse("01/01/" + JulianDate.Substring(0, 4) + " 00:00:00")
 dtOut = dtOut.AddDays(Integer.Parse(JulianDate.Substring(4).Trim()))
 Return dtOut
End Function

Open in new window

0
 
Zeus2009Author Commented:
Hi,
Thanks for the reply, the problem is, it is a Julian Day which is a double http://en.wikipedia.org/wiki/Julian_day 

for instance i need to convert 1268362.1231231 to a date time

am i right in saying the first part of the Double is the date and then the second part is the time?

Thanks
Zeus
0
 
Zeus2009Author Commented:
A Julian date of 2454115.05486 means that the date and Universal Time is Sunday January 14, 2007 at 13:18:59.9.
0
 
DhaestCommented:
You only need to add some formatting to display the date like you want
        private DateTime ConvertJulianToDateTime(double julianDate)
        {
            DateTime date;
            double dblA, dblB, dblC, dblD, dblE, dblF;
            double dblZ, dblW, dblX;
            int day, month, year;
            try
            {
                dblZ = Math.Floor(julianDate + 0.5);
                dblW = Math.Floor((dblZ - 1867216.25) / 36524.25);
                dblX = Math.Floor(dblW / 4);
                dblA = dblZ + 1 + dblW - dblX;
                dblB = dblA + 1524;
                dblC = Math.Floor((dblB - 122.1) / 365.25);
                dblD = Math.Floor(365.25 * dblC);
                dblE = Math.Floor((dblB - dblD) / 30.6001);
                dblF = Math.Floor(30.6001 * dblE);
                day = Convert.ToInt32(dblB - dblD - dblF);
                if (dblE > 13)
                {
                    month = Convert.ToInt32(dblE - 13);
                }
                else
                {
                    month = Convert.ToInt32(dblE - 1);
                }
                if ((month == 1) || (month == 2))
                {
                    year = Convert.ToInt32(dblC - 4715);
                }
                else
                {
                    year = Convert.ToInt32(dblC - 4716);
                }
                date = new DateTime(year, month, day);

                string calculateHour = julianDate.ToString();
                double seconds = 0;
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 1, 1)) * 8640);
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 2, 1)) * 864);
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 3, 1)) * 86.4);
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 4, 1)) * 8.64);
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 5, 1)) * 0.864);
                date = date.AddSeconds(seconds);

                return date;
            }
            catch (ArgumentOutOfRangeException ex)
            {
                MessageBox.Show("Julian date could not be converted:\n" + ex.Message, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                date = new DateTime(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error converting Julian date:\n" +
                ex.Message, "Conversion Error", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
                date = new DateTime(0);
            }
            return date;
        } 

Open in new window

0
 
Zeus2009Author Commented:
Hi,
The code supplied goes a little way to converting the julian date/time but just the date part, i need it to convert to a correct datetime, is there any other samples that work correctly?

Thanks
Zeus
0
 
DhaestCommented:
I tested the latest code on my machine and I got the date & time back in the datatime !

double julian = 2454115.05486;
dateTime julianDate = ConvertJulianToDateTime(julian);

If I take a look at the variable julianDate, I see all that you asked for, namely January 14, 2007 at 13:18:59 (I didn't look at the milliseconds, but I believe they will be also there)
0
 
Zeus2009Author Commented:
Hi Dhaest,

Thanks for the reply but the method ConvertJulianToDateTime is not avaliable in Windows Mobile Compact framework 2,

If you can provide a working example that does not use the above method i will assign 500 points.

Thanks
Zeus
0
 
DhaestCommented:
ConvertJulianToDateTime is a function, written by myself (the code is posted below again)
private DateTime ConvertJulianToDateTime(double julianDate)
        {
            DateTime date;
            double dblA, dblB, dblC, dblD, dblE, dblF;
            double dblZ, dblW, dblX;
            int day, month, year;
            try
            {
                dblZ = Math.Floor(julianDate + 0.5);
                dblW = Math.Floor((dblZ - 1867216.25) / 36524.25);
                dblX = Math.Floor(dblW / 4);
                dblA = dblZ + 1 + dblW - dblX;
                dblB = dblA + 1524;
                dblC = Math.Floor((dblB - 122.1) / 365.25);
                dblD = Math.Floor(365.25 * dblC);
                dblE = Math.Floor((dblB - dblD) / 30.6001);
                dblF = Math.Floor(30.6001 * dblE);
                day = Convert.ToInt32(dblB - dblD - dblF);
                if (dblE > 13)
                {
                    month = Convert.ToInt32(dblE - 13);
                }
                else
                {
                    month = Convert.ToInt32(dblE - 1);
                }
                if ((month == 1) || (month == 2))
                {
                    year = Convert.ToInt32(dblC - 4715);
                }
                else
                {
                    year = Convert.ToInt32(dblC - 4716);
                }
                date = new DateTime(year, month, day);

                string calculateHour = julianDate.ToString();
                double seconds = 0;
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 1, 1)) * 8640);
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 2, 1)) * 864);
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 3, 1)) * 86.4);
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 4, 1)) * 8.64);
                seconds += (double.Parse(calculateHour.Substring(calculateHour.IndexOf(",") + 5, 1)) * 0.864);
                date = date.AddSeconds(seconds);

                return date;
            }
            catch (ArgumentOutOfRangeException ex)
            {
                MessageBox.Show("Julian date could not be converted:\n" + ex.Message, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                date = new DateTime(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error converting Julian date:\n" +
                ex.Message, "Conversion Error", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
                date = new DateTime(0);
            }
            return date;
        } 

Open in new window

0
 
Zeus2009Author Commented:
Hi,

I have tried this function multiple times and stepped through the code, it keeps reporting the time as 5:53:32 AM.

Thanks
Zeus
0
 
Zeus2009Author Commented:
Ok i tried all kind of solutions and none worked out of the box except the attached code.

Thanks for the replies

Zeus
   Public Function fromJulian(ByVal JDt As Double) As DateTime
        'fromJulian - returns the date based on a julian date
        'VB .Net implementation of:
        'based on - http://www.astro.uu.nl/~strous/AA/en/reken/juliaansedag.html
        'with time code added

        Dim RetVal As DateTime
        Dim d, e, h, i, j, m, n, p, q, s1, s2, s3, s4 As Double
        j = JDt
        p = Math.Floor(j + 0.5)
        s1 = p + 68569
        n = Math.Floor(4 * s1 / 146097)
        s2 = s1 - Math.Floor((146097 * n + 3) / 4)
        i = Math.Floor(4000 * (s2 + 1) / 1461001)
        s3 = s2 - Math.Floor(1461 * i / 4) + 31
        q = Math.Floor(80 * s3 / 2447)
        e = s3 - Math.Floor(2447 * q / 80)
        s4 = Math.Floor(q / 11)
        m = q + 2 - 12 * s4
        j = 100 * (n - 49) + i + s4
        d = e + JDt - p + 0.5
        h = d - (Math.Floor(d))
        d = d - h
        RetVal = DateSerial(CInt(j), CInt(m), CInt(d))
        RetVal = RetVal.AddDays(h)
        Return (RetVal)
    End Function

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now