|
This function will convert a
Gregorian calendar date to a Julian Day.
The function 'jd' does the actual
conversion. The lengthy code above it is a sample of
how
to use the function in a script. The code accepts
input from the command-line of one or
more date and time strings in the format mm/dd/yy@hh:mm
(you can change both the date
part separator and the time part separator). The
time portion is optional - if not given, time is
assumed to be 00:00:00 hours UTC. If no
command-line arguments are given then the script
assumes the current day and time.
The script returns the Julian day
for the input given or -1 if the input string was
invalid.
Since a Julian day represents an
offset as a number of days with a fractional part to
represent
the time in UTC format you need to modify the
timezone constant, offset, to the number of minutes
from UTC for your timezone. You should also modify
the constants datesep & timesep to suit your locale.
The input format, mm/dd/yy[@hh:mm]
may seem a bit awkward but I chose it to illustrate
how to use the split function and the WSH Regular
Expression object to split the input data
into its component parts and to validate it. Since
the function jd does the actual conversion
you can, of course, interface to it in any wish you
wish. Just remember that the function itself
does not validate the input data. That remains your
responsibility.
For years BCE (Before the Common Era) use a negative
year such as in 1/1/-1234
Note, if you use - as the date separator you can not
enter BCE dates.
Sample usage and output:
julianday.vbs
2452654.06877
julianday.vbs 5/12/1956@10:15
test 10/10/1001
2435605.92708
-1
2086955.5
' Convert a Gregorian Date to a
Julian Day
' the function 'jd' adapted to vbscript from a
jscript
' function by Dan Burton from a web page at
' http://www.isc.tamu.edu/~astro/javascript/julianday.html
'Timezone offset in minutes from UTC
Const offset=-300
'Character used to seperate date components
(normally - or /)
Const datesep = "/"
'Character used to seperate time components
(normally : or ,)
Const timesep = ":"
if offset < 0 then
off = abs(offset)
else
off = 0 - offset
end if
args = WScript.Arguments.Count
if args = 0 then
UTCNow = dateadd("n", off, Now)
y = Year(UTCNow)
m = Month(UTCNow)
d = Day(UTCNow)
h = Hour(UTCNow)
mn = Minute(UTCNow)
sec = Second(UTCNow)
wscript.echo jd(y, m, d, h, mn, sec)
else
for x = 0 to args - 1
TheDate = split(WScript.Arguments.Item(x), "@")
Set regEx = New RegExp ' Create regular
expression.
regEx.Pattern = "^[0-9]{1,2}" & datesep &
"[0-9]{1,2}" & datesep & "[+-]*[0-9]{1,4}$"
regEx.Global = True
set matches = regEx.Execute(TheDate(0))
if matches.count <> 1 then
wscript.echo -1
else
ThisDate = split(TheDate(0), datesep)
if UBound(TheDate) = 1 then
TheTime = Trim(TheDate(1))
Set regEx = New RegExp ' Create regular
expression.
regEx.Pattern = "^[0-9]{1,2}" & timesep
&"[0-9]{1,2}$"
regEx.Global = True
set matches = regEx.Execute(TheTime)
if matches.count = 1 and CInt(ThisDate(2))
<> 0 then
if CInt(ThisDate(2)) = 1582 and
CInt(ThisDate(0)) = 10 and (Cint(ThisDate(1)) > 4
and CInt(ThisDate(1)) < 15) then
wscript.echo -1
else
ThisTime = split(TheTime, ":")
wscript.echo jd(CInt(ThisDate(2)),
CInt(ThisDate(0)), CInt(ThisDate(1)),
CInt(ThisTime(0)), CInt(Thistime(1)), 0)
end if
else
wscript.echo -1
end if
else
if CInt(ThisDate(2)) = 1582 and
CInt(ThisDate(0)) = 10 and (Cint(ThisDate(1)) > 4
and CInt(ThisDate(1)) < 15) then
wscript.echo -1
else
wscript.echo jd(CInt(ThisDate(2)),
CInt(ThisDate(0)), CInt(ThisDate(1)), 0, 0, 0)
end if
end if
end if
next
end if
'function usage: jd(YEAR, MONTH, DAY of MONTH, HOUR,
MINUTES, SECONDS)
function jd(yy, mm, dd, hr, mn, sec)
'correct original which didn't properly handle BCE
dates
if yy < 0 then
yy = yy + 1
end if
hr = hr + (mn / 60) + sec/3600
ggg = 1
if yy <= 1585 then
ggg = 0
end if
jd = -1 * Int(7 * (Int((mm + 9) / 12) + yy) / 4)
s = 1
if (mm - 9) < 0 then
s = -1
end if
a = abs(mm - 9)
j1 = Int(yy + s * Int(a / 7))
j1 = -1 * Int((Int(j1 / 100) + 1) * 3 / 4)
jd = jd + Int(275 * mm / 9) + dd + (ggg * j1)
jd = jd + 1721027 + 2 * ggg + 367 * yy - 0.5
jd = round(jd + (hr / 24), 5)
end function |
|