PaulSadowski.com 

Windows Scripting Host
 VBScipt Function to Convert a Julian Day to a Gregorian Date
This function will convert a Julian date number to a Gregorian calendar date string.

Sample usage:
  wscript.echo jd2greg(2435605.92708)

Sample output:
  5-12-1956 10:14:60

You can modify the output format by changing the last line of the function. 


' adapted from the function jd_to_cal by Marc A. Murison
' Astronomical Applications Dept. U.S. Naval Observatory
' 3450 Massachusetts Ave, NW Washington, DC 20392-5420
' http://aa.usno.navy.mil/data/docs/JulianDate.html


Function jd2greg(jd)

intgr = int(jd)
frac = jd - intgr
gregjd = 2299161

if(intgr >= gregjd) then
  tmp = int(((intgr - 1867216) - 0.25) / 36524.25)
  j1 = intgr + 1 + tmp - int(0.25*tmp)
else
  j1 = intgr
end if

'correction for half day offset
dayfrac = frac + 0.5
if(dayfrac >= 1.0) then
  dayfrac = dayfrac - 1
  j1 = j1 + 1
end if

j2 = j1 + 1524
j3 = int(6680.0 + ((j2 - 2439870) - 122.1)/365.25)
j4 = int(j3*365.25)
j5 = int((j2 - j4)/30.6001)

d = int(j2 - j4 - int(j5*30.6001))
m = int(j5 - 1)
if(m > 12) then
  m = m -12
end if
y = int(j3 - 4715)
if(m > 2) then
  y = y -1
end if
if(y <= 0) then
  y = y - 1
end if

' get time of day from day fraction
hr = int(dayfrac * 24.0)
mn = int((dayfrac*24.0 - hr)*60.0)
f = ((dayfrac*24.0 - hr)*60.0 - mn)*60.0
sc = int(f)
f = sc -1
if(f > 0.5) then
  sc = sc + 1
end if
if(y < 0) then
  y = -y
end if
 

jd2greg = m & "-" & d & "-" & y & " " & hr & ":" & mn & ":" & sc
 

End Function

 

 

© 2003 by Paul R. Sadowski   
All Rights Reserved. Used By Permission.  
Comments to: Scripting