Ruby Code for Calculating the Julian Day

Subscribe to my Feed!

Here is a quick ruby script which can be used for calculating the Julian day. You can either download it by clicking here or simply copy and paste it.

These calculations were taken from the book Astronomical Algorithms by Jean Meeus, pages 59-61. It’s a good book to have a copy of if you’re into math, programming, astronomy, or just science stuff in general.

# Begin Julian Day Calculation (Meeus Pages 59-61) vvvv
t= Time.now
 
####
#You can change these variables and calculate the JD for a specific #date if you want. Be sure though that you compensate for
#your time zone if changing the hour.
y = t.gmtime.year
m = t.gmtime.month
d = t.gmtime.day + t.gmtime.hour/24.to_f + t.gmtime.min/1440.to_f + t.gmtime.sec/86400.to_f
####
 
if m>2
y=y
m=m
else
y = y-1
m = m+12
end
a = (y/100).to_int
b = 2 - a + (a/4).to_int
jd = (365.25 * (y+4716)).to_int + (30.6001 * (m+1)).to_int + d + b + -1524.5
# End Julian Day Calculation^^^^
 
puts jd

Subscribe to my Feed!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">