How to know the day name of a Time object in Ruby
Overview
In Ruby, we have several similar methods to know the day name of a time object. The technique is the same for all: timeObject.dayName?. Here, the dayName can be any day of the week, for example, thursday, wednesday, monday, and so on.
Syntax
timeObject.dayName?
Parameters
timeObject: This is the Time object we want to check.
dayName: This is the method name. It could be any day of the week.
Return value
The value returned is a Boolean value. It can be true or false. true is returned if timeObject occurs on dayName. Otherwise, a false is returned.
Example
# create time objectstimeObjectOne = Time.now # current timetimeObjectTwo = Time.new(1995, 12, 21) # Thursday in 1990timeObjectThree = Time.new(1990, 4, 1) # Sunday in 1998timeObjectFour = Time.new(1987, 12, 19) # Saturday December, 1987timeObjectFive = Time.new(2022, 01, 21) # Friday January, 2022# check day nameputs timeObjectOne.monday?puts timeObjectTwo.thursday?puts timeObjectThree.sunday?puts timeObjectFour.saturday?puts timeObjectFive.wednesday?
Explanation
Line 2–6: We create the following Time objects:
timeObjectOnetimeObjectTwotimeObjectThreetimeObjectFourtimeObjectFive
Line 9–13: Some of the methods that we explained earlier are called on the Time objects we created. We print the results to the console.