1.6 Date
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
.
1.6.1 Creating a date
The Date
class isn’t loaded into every Ruby program by default, so to use it we first need to say
"date" require
(Usually we omit the parentheses around the string argument to the require
method. Just like when we use p "Hello, world!"
as opposed to p("Hello, World!")
.)
1.6.1.1 Date.new
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! Interesting Year 1 of the Julian Period was 4713 BC (−4712)., 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 ...>
1.6.2 Date Methods
Date.today
The Date.today
method returns an object initialized to the current date.
Date.today # => #<Date: 2019-04-16 ((2458590j,0s,0n),+0s,2299161j)>
year
Call the year
method on a Date
object to return just the year of the date as an Integer
.
Date.today # => #<Date: 2019-04-16 ((2458590j,0s,0n),+0s,2299161j)>
t = # => 2019 t.year
month
Call the month
method on a Date
object to return just the month of the date as an Integer
.
Date.today # => #<Date: 2019-04-16 ((2458590j,0s,0n),+0s,2299161j)>
t = # => 4 t.month
day
Call the day
method on a Date
object to return just the day of the date as an Integer
.
Date.today # => #<Date: 2019-04-16 ((2458590j,0s,0n),+0s,2299161j)>
t = # => 16 t.day
Open the GitPod Date
project for this chapter and start with the exercise formatted.rb
:
LTI{Load assignment}(https://github.com/bpurinton-appdev/date-chapter/tree/bp-additions)[MV4dKHMwdAFhfRn752YW3TAY]{KBpPhe42o6wDRi35rWagKY4F}(20)[date_project]
For a GitPod refresher, see here.
Date.parse
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 ...>
Subtraction
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
:
Date.today - Date.parse("July 4, 1776")
number_of_days = 88674/1)
=> (
days.to_i88674 =>
Return to the GitPod Date
project and work through math.rb
1.6.3 Time
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
strftime
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.