In Ruby, the jd()
method generates a date object indicating the given chronological Julian day number. To get the chronological Julian day number, use Date.jd()
method.
jd()
Method in Ruby
jd()
method exists in Standard Date Library of Ruby. The purpose of this method is to generate a date object indicating the given chronological Julian day number.
Syntax of the jd()
Method in Ruby
In date, the syntax of jd()
method is:
Date.jd(x)
Parameter of jd()
Method in Ruby
x
is any valid Julian date object. This parameter is required.Note: If the
x
parameter is not a valid Julian date object jd()
method will return an error and if it is zero, it will show the start of the calendar like -4712-01-01.
Return Value of jd()
Method
jd()
method generates a new date object by indicating from a string given in a code.
Julian Date Format
The date format for Julian (JUL) is CYYDDD.
Here YY represents the year and the DDD indicates the day of the year.
Here YY represents the year and the DDD indicates the day of the year.
How to Get the Julian Day Number in Ruby?
# import date library require 'date' x = Date.jd(2457000) puts x
Output
2014-12-08
# import date library require 'date' x = Date.jd(0) puts x
Output
-4712-01-01
Note: In the above code (0) represents the start of the Julian Calendar Date as given in the output.
How to convert Julian Date to Calendar Date?
To covert the Julian date to current Calendar Date see the following example.
# import date library require 'date' julian = "14125" puts Date.strptime(julian, "%y%j")
Output
2014-05-05
NOTE: In the above code (14125), 14 represent the year and 125 is represents the day of the year.
How to Convert Current Date and Time into Julian Date Number?
# import date library require 'date' current_date = DateTime.now.amjd().to_f # Date: 2016-15-07 puts current_date
Output
57959.19037960804
NOTE: The amjd()
function is used to convert the current date into Julian date and then converting the output into float is helpful to understand easy.