Sure, we could just use a String
to represent a date, like "April 19, 1987"
. But Ruby has a built-in class that makes it much easier to work with dates: Date
.
The Date
class isn’t loaded into every Ruby program by default, so to use it we first need to say
require "date"
(Usually we omit the parentheses around the string argument to the require
method.)
After require "date"
, we can create a new instance as usual with:
Date.new # => #<Date: -4712-01-01 ((0j,0s,0n),+0s,2299161j)>
By default, the new date is January 1st, of the year -4712 BCE! Interesting1, but not very helpful.
You can also pass Date.new
arguments to initialize with a specific year, month, and day:
Date.new(2001) #=> #<Date: 2001-01-01 ...>
Date.new(2001,2,3) #=> #<Date: 2001-02-03 ...>
Date.new(2001,2,-1) #=> #<Date: 2001-02-28 ...>
The Date.today
method returns an object initialized to the current date.
Date.today # => #<Date: 2019-04-16 ((2458590j,0s,0n),+0s,2299161j)>
Call the year
method on a Date
object to return just the year of the date as an Integer
.
t = Date.today # => #<Date: 2019-04-16 ((2458590j,0s,0n),+0s,2299161j)>
t.year # => 2019
Call the month
method on a Date
object to return just the month of the date as an Integer
.
t = Date.today # => #<Date: 2019-04-16 ((2458590j,0s,0n),+0s,2299161j)>
t.month # => 4
Call the day
method on a Date
object to return just the day of the date as an Integer
.
t = Date.today # => #<Date: 2019-04-16 ((2458590j,0s,0n),+0s,2299161j)>
t.day # => 16
The Date.parse()
method accepts a String
argument and tries to interpret it as a date, initializing a Date
object.
Date.parse("2001-02-03") #=> #<Date: 2001-02-03 ...>
Date.parse("20010203") #=> #<Date: 2001-02-03 ...>
Date.parse("3rd Feb 2001") #=> #<Date: 2001-02-03 ...>
You can subtract two dates from one another, which will return the number of days between them. The return value class is a Rational
, which can be converted to a regular Integer
with .to_i
:
number_of_days = Date.today - Date.parse("July 4, 1776")
=> (88674/1)
days.to_i
=> 88674
Returns true
if the date is a Monday.
Returns true
if the date is a Tuesday.
Returns true
if the date is a Wednesday.
Returns true
if the date is a Thursday.
Returns true
if the date is a Friday.
Returns true
if the date is a Saturday.
Returns true
if the date is a Sunday.
Returns the day of week (0-6, Sunday is zero).
Date.new(2001,2,3).wday #=> 6
Ruby has a Time
class as well, that shares most of its methods with the Date
class.
Time.now.wday # => 6
Time.now.saturday? # => true
Time.now.day # => 3
The strftime
method is used on a Date
or Time
object. It requires a String
argument that will be used to format the Date or Time in a particular way.
Assuming today is Monday, September 7th 2020
Time.now.strftime("%A") # => "Monday"
Time.now.strftime("%B") # => "September"
Time.now.strftime("%b") # => "Sep"
Time.now.strftime("%a %e, %R %p") # => "Mon, 7 14:35 PM"
You should not try to memorize what these patterns mean. Tools like strftime.net and For a Good Strftime exist to help compose the formatting string argument.