# Thread: [RESOLVED] Converting Gregorian to JDE Julian & back

1. ## [RESOLVED] Converting Gregorian to JDE Julian & back

I'm looking to be able to create a small .Net program that would allow me to convert a Gregorian calendar date to a Julian date and visa versa. For example today (6-April-2012) Gregorian is 112097 Julian. The Julian date structure is:

1 digit for the century
2 digits for the current year
3 digits for the current day of the year

I've tried a few online converters but they show today as being 2456023.5 Julian, but all of the records for this morning in the JDE inventory tracking program at work says today is 112097, so I would like to make my own program rather than using an online converter.

2. ## Re: Converting Gregorian to JDE Julian & back

Ok, I've done some playing with it and it looks like I simply parse the year:
For the century I take the 2nd digit in the year and I add 1 to it, so for 2012 I take the 0 and add 1.
For the current year, I just take the last 2 digits from the year, which is the 12.
For the day of the year, luckily .Net's Date object has a DayOfYear property I can use, which for today returns "97", all I have to do is format it in a "###" fasion so 97 shows as 097.

Now the hard part, taking "112097" and parsing it into a "06/04/2012" date.

5. ## Re: [RESOLVED] Converting Gregorian to Julian & back

drop the century digit... take the year, use it to build the date 1/1 for that year... then use the DateAdd (or .AddDays if using .NET) to add the number of days (less 1) to the 1/1 date... that should then return the correct date.

-tg

Juggalo - Hold on a moment there.... in your post, you said:
"For the century I take the 3rd digit in the year and I add 1 to it, so for 2012 I take the 0 and add 1." -- but isn't the third digit "1" ?? 2-0-1-2 ... or did you mean the "second" digit in the year?

-tg

6. ## Re: [RESOLVED] Converting Gregorian to JDE Julian & back

Originally Posted by rlawoo
The Julian date I was needing is a JDE Julian date and not a true Julian date, fyi:
vb Code:
`#Region " Date Conversions: GetGregorianDate, GetJDEDate "     Private Function GetGregorianDate(ByVal JDEDate As String) As Date        Dim GregDate As Date = DateTime.Today        JDEDate = JDEDate.Trim        If JDEDate.Length > 0I AndAlso Long.TryParse(JDEDate, New Long) Then            Dim DayOfYearStr As String = If(JDEDate.Length > 3I, JDEDate.Substring(JDEDate.Length - 3I, 3I), JDEDate)            If JDEDate.Length > 3I Then                JDEDate = JDEDate.Substring(0I, JDEDate.Length - 3I)                Dim YearStr As String = If(JDEDate.Length > 2I, JDEDate.Substring(JDEDate.Length - 2I, 2I), JDEDate)                If JDEDate.Length > 2I Then                    JDEDate = JDEDate.Substring(0I, JDEDate.Length - 2I)                    Dim Century As Integer = CInt(JDEDate) + 19I                    GregDate = New Date(CInt(Century.ToString & YearStr), 1I, 1I)                Else                    'No century was provided                    GregDate = New Date(CInt("19" & CInt(YearStr).ToString("00")), 1I, 1I)                End If            Else                'DayOfYear is all that was provided                GregDate = New Date(m_MinDate.Year, m_MinDate.Month, m_MinDate.Day)            End If            GregDate = GregDate.AddDays(CInt(DayOfYearStr) - 1I)        End If        Return GregDate    End Function     Private Function GetJDEDate(ByVal GregorianDate As Date) As String        Return String.Format("{0}{1}{2}", If(GregorianDate.Year < 2000I, String.Empty, CStr(CInt(GregorianDate.Year.ToString.Substring(1I, 1I)) + 1I)), GregorianDate.Year.ToString.Substring(2I, 2I), GregorianDate.DayOfYear.ToString("000")).TrimStart("0"c)    End Function     Private Function IsJDEDate(ByVal dte As String) As Boolean        Dim Output As Boolean = False        dte = dte.Trim        If dte.Length > 0I AndAlso Long.TryParse(dte, New Long) Then            Dim DayOfYearStr As String = If(dte.Length > 3I, dte.Substring(dte.Length - 3I, 3I), dte)            Dim DayOfYearInt As Integer = Integer.Parse(DayOfYearStr)            If DayOfYearInt >= 0I AndAlso DayOfYearInt <= 366 Then                If dte.Length > 3I Then                    dte = dte.Substring(0I, dte.Length - 3I)                    Dim YearStr As String = If(dte.Length > 2I, dte.Substring(dte.Length - 2I, 2I), dte)                    Dim YearInt As Integer = Integer.Parse(YearStr)                    If YearInt >= 0I Then                        If dte.Length > 2I Then                            dte = dte.Substring(0I, dte.Length - 2I)                            If Integer.Parse(dte) >= 0I Then Output = True                        Else                            Output = True                        End If                    End If                Else                    Output = True                End If            End If        End If        Return Output    End Function #End Region`

Originally Posted by techgnome
drop the century digit... take the year, use it to build the date 1/1 for that year... then use the DateAdd (or .AddDays if using .NET) to add the number of days (less 1) to the 1/1 date... that should then return the correct date.

-tg

Juggalo - Hold on a moment there.... in your post, you said:
"For the century I take the 3rd digit in the year and I add 1 to it, so for 2012 I take the 0 and add 1." -- but isn't the third digit "1" ?? 2-0-1-2 ... or did you mean the "second" digit in the year?

-tg
Post edited

7. ## Re: [RESOLVED] Converting Gregorian to JDE Julian & back

thank you very much both of you

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured