Aditya Landge
Aditya Landge

Reputation: 1245

Julian Date and Time to regular Date and Time

I am receiving Julian Date and Time from jde table as:

SLUPMJ(Date) | SLTDAY(Time) 
---------------------------
116173       | 94959

I need to convert them into regular Date and Time values to integrate into my application.

While I am able to convert the date part using custom code:

DCY = Left(Date1, 3)

If Left(DCY, 1) = "0" Then
    DYear = "19" & Right(DCY, 2)
Else
    DYear = "20" & Right(DCY, 2)
End If

Dinterval = CInt(Right(Date1, 3))

TDate = DateAdd("d", Dinterval - 1, "01/01/" & DYear)
J2D2 = Day(TDate) & "/" & Month(TDate) & "/" & Year(TDate)
        

How do I convert the Time part in vb.net?

Upvotes: 0

Views: 1229

Answers (1)

Major Fifth
Major Fifth

Reputation: 90

Here's some code to convert the time portion of a Julian datetime. There is probably a simpler way, but Coding should be hard!™ :)

''' <summary>
''' Converts the time portion of a Julian datetime
''' </summary>
''' <param name="dJDateTime">The Julian datetime. For example: 2457984.021181</param>
''' <returns>The time, formatted as follows: d:hh:mm:ss.mmm</returns>
Function ConvertJulianTime(ByVal dJDateTime As Decimal) As String
    ' Julian time is calculated as a fraction of a 24-hour period, starting from noon. 
    ' So a value >= .5 is a morning hour on the next day

    ' Get the fractional part of the Julian datetime, which is the time
    Dim dJTime As Decimal = GetFractionalPart(dJDateTime)
    ' Get time in seconds
    Dim dJTimeInSec As Decimal = 86400 * dJTime + 43200 ' 43200 is half a day in seconds
    ' Calculate the hours
    Dim dJTimeInHours As Decimal = dJTimeInSec / 3600
    ' If the value is >= 24, it's a morning hour so increment the day value
    Dim intDay As Integer = 0
    If dJTimeInHours >= 24 Then
        dJTimeInHours = dJTimeInHours - 24
        intDay = 1
    End If
    ' Calculate the minutes
    Dim dJTimeInHoursMin As Decimal = GetFractionalPart(dJTimeInHours) * 60
    ' Calculate the seconds
    Dim dJTimeInHoursSec As Decimal = GetFractionalPart(dJTimeInHoursMin) * 60
    ' Calculate the milliseconds
    Dim dJTimeInHoursMSec As Decimal = GetFractionalPart(dJTimeInHoursSec) * 1000
    ' Build the result and display it
    Dim tsSpan As New TimeSpan(intDay, CInt(Math.Truncate(dJTimeInHours)),
                               CInt(Math.Truncate(dJTimeInHoursMin)),
                               CInt(Math.Truncate(dJTimeInHoursSec)),
                               CInt(Math.Truncate(dJTimeInHoursMSec)))
    Return tsSpan.ToString("d\:hh\:mm\:ss\.fff")
End Function
''' <summary>
''' Given a decimal, gets the fractional part only. For example, for 1234.5678, returns 0.5678
''' </summary>
''' <param name="dNumber">The decimal.</param>
''' <returns>The fractional part of the decimal.</returns>
Function GetFractionalPart(ByVal dNumber As Decimal) As Decimal
    Dim dInt As Decimal = Math.Truncate(dNumber)
    Return (dNumber - dInt)
End Function

End Class

Upvotes: 1

Related Questions