Solved

Julian Date Convertion (2454115.05486)

Posted on 2010-08-18
14
1,136 Views
Last Modified: 2013-11-27
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
Comment
Question by:Zeus2009
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33464975
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33464982
using System;
 
namespace DateFromDouble
{
    class Program
    {
        static void Main(string[] args)
        {
                Console.WriteLine(DateTime.FromOADate(39820.0));
        }
    }
}
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33465006
  ///<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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 11

Expert Comment

by:ladarling
ID: 33465012
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33465312
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
 

Author Comment

by:Zeus2009
ID: 33475799
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
 

Author Comment

by:Zeus2009
ID: 33475825
A Julian date of 2454115.05486 means that the date and Universal Time is Sunday January 14, 2007 at 13:18:59.9.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33482673
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
 

Author Comment

by:Zeus2009
ID: 33485560
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 33486351
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
 

Author Comment

by:Zeus2009
ID: 33487060
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 33487132
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
 

Author Comment

by:Zeus2009
ID: 33490811
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
 

Accepted Solution

by:
Zeus2009 earned 0 total points
ID: 33497124
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question