Help with julian date?

F

FatMan

Hi all:
I would like to know if there is a function or a piece of code that will
take a jullian date (the day of the year 1 for Jan 1st, 31 for Jan 31st, 100
for April 10th, 180 for Jun 29th, 365 Dec 31st, etc.) and convert it to the
date it represents.

Is this possible?

Any help is greatly appreciated.

Thanks,
FatMan
 
D

Dirk Goldgar

FatMan said:
Hi all:
I would like to know if there is a function or a piece of code that will
take a jullian date (the day of the year 1 for Jan 1st, 31 for Jan 31st,
100
for April 10th, 180 for Jun 29th, 365 Dec 31st, etc.) and convert it to
the
date it represents.

Is this possible?


The conversion is easy enough with a very little bit of code, but how is the
Julian date actually provided? Does it include the year, or should one
assume the current year? If it includes the year, does it include all four
digits, or should the standard Windows guess be made for two-digit years?
 
D

Dirk Goldgar

Dirk Goldgar said:
The conversion is easy enough with a very little bit of code, but how is
the Julian date actually provided? Does it include the year, or should
one assume the current year? If it includes the year, does it include all
four digits, or should the standard Windows guess be made for two-digit
years?


For example, suppose your Julian date is specified as a string in the form
"YYYY.DDD" or "YY.DDD". Then you could use a function like this:

'------ start of code ------
Function JulianToGregorian(JulianDate As Variant) As Variant

' Convert Julian date (year.day format)into
' Gregorian date (as standard date/time value).
'
' Arguments:
' JulianDate - Variant(String) - input Julian date
' as string in year.day format. May be Null.
' Year values under 100 are interpreted as 2-digit
' years, and will be windowed according to the
' system settings.
'
' Return value:
' Corresponding Gregorian date as a standard date/time
' data type. Null if input was Null, Error(5) if input
' was invalid.

On Error GoTo Err_Handler

Dim strJulDate As String
Dim intDot As Integer
Dim intYear As Integer
Dim intDay As Integer

' Convert Julian data to string if necessary.
strJulDate = JulianDate & vbNullString

If Len(strJulDate) = 0 Then
JulianToGregorian = Null
Else
intDot = InStr(1, strJulDate, ".", vbBinaryCompare)
intYear = CInt(Left$(strJulDate, intDot - 1))
intDay = CInt(Mid$(strJulDate, intDot + 1))
JulianToGregorian = DateSerial(intYear, 1, intDay)
End If

Exit_Point:
Exit Function

Err_Handler:
JulianToGregorian = CVErr(5)
Resume Exit_Point

End Function
'------ end of code ------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top