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
Solved

Julian Date Convertion (2454115.05486)

Posted on 2010-08-18
14
1,106 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

808 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