Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Julian Date 1

Status
Not open for further replies.

pkailas

Programmer
Jun 10, 2002
555
US
Does anyone have a script to convert a Julian date to mm/dd/yyyy format?

105140 = 05/19/2005

Thanks in advance.

_______
I love small animals, especially with a good brown gravy....
 
A starting point:
Function jul2date(jd)
Dim y, j, a, m
j = 0 + Right(jd, 3)
y = 1900 + Left(jd, Len(jd) - 3)
If y Mod 4 = 0 And (y Mod 100 <> 0 Or y Mod 400 = 0) Then
a = Array(31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 336, 366)
Else
a = Array(31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365)
End If
For m = 13 To 2 Step -1
If a(m - 2) < j Then Exit For
Next
jul2date = CDate(y & "-" & m & "-" & j - a(m - 2))
End Function
MsgBox "105140 = " & jul2date("105140")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Not a type of Julian date I am particularly familiar with, but the following may be of use:

Function jul2date(jd)
' from info provided #5/19/2005# - 105140 generates base date
jul2datems = CDate(jd + #5/19/2005# - 105140 + 1)
End Function

MsgBox "105140 = " & jul2date(105140)
 
You could try
Code:
Function j2d(jd As String) As Date

  j2d = DateAdd("d", jd Mod 1000, "12/31/" & CStr(jd \ 1000 + 1899))

End Function

However day 140 in 2005 is 20th May not 19th May

Hope this helps.


 
PHV and strongm, I've just tested your suggestions:

PHV - 105001 gives subscript out of range error
strongm - 104001 gives 07/04/2002 or US 04/07/2002 instead of Jan 1st 2004

The format of this type of Julian date is:

Year - 1900 concatenated with day number (where 1 is Jan 1st)

Therefore:
Year = JulDate integer divided by 1000
DayOfYear = JulDate Mod 1000
Since DayOfYear is 1 based (as opposed to 0 based) it is necessary to add DayOfYear to 31st December Year - 1 to return the correct date.

Hope this helps.
 
Good catch earthandfire, here a modified version:
Function jul2date(jd)
Dim y, j, a, m
j = 0 + Right(jd, 3)
y = 1900 + Left(jd, Len(jd) - 3)
If y Mod 4 = 0 And (y Mod 100 <> 0 Or y Mod 400 = 0) Then
a = Array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 336, 366)
Else
a = Array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365)
End If
If j > a(12) Then Exit Function
For m = 14 To 3 Step -1
If a(m - 2) < j Then Exit For
Next
jul2date = CDate(y & "-" & m - 1 & "-" & j - a(m - 2))
End Function

But I like yours with Mod and integral division.
 
PHV

I thought you might find this interesting (or not [smile])

?j2d(-1799364)
01/01/100
?j2d(8099365)
31/12/9999

By the way, my description of the Julian Date format was wrong, it should have been:

Year - 1900 concatenated with three digit day number (where 001 is Jan 1st).

Also see negative logic above for dates before 31/12/1899
 
PHV,

I had tried your first one and I didn't get any errors... however, I'll test the 2nd.

BTW, I have to actually use this in an SBL script (Kofax Ascent Capture) ack!

I'm hoping that the vbscript functions are supported... I'll let you know.. heh


_______
I love small animals, especially with a good brown gravy....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top