Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

How to know if today or a Time Object is Sunday in Ruby

okwudili onyejiaku

Overview

We use the sunday? method to learn if today or a time object is Sunday or represents Sunday. The method returns a Boolean value true or false. If the time object is Sunday, true is returned. Otherwise, false.

Syntax

t_o.sunday?
Syntax to Check if Today is Sunday

Parameters

t_o: This is the time object we want to check for representing Sunday.

Return value

The value returned is a Boolean value. If the time object is Sunday, true will be returned. Otherwise, false will be returned.

Code example

# create time objects
t1 = Time.now  # current time
t2 = Time.new(1990)
t3 = Time.new(1990, 4, 1) # sunday in 1998
t4 = Time.new()

# call the "sunday?" method
# and print result
puts t1.sunday?  
puts t2.sunday?  
puts t3.sunday?  
puts t4.sunday?  
Check if a Time Object is a Sunday

Explanation

  • line 2-5: We create time objects using the Time.now or the Time.new() methods.
  • line 9-12: We use the sunday? method to check if the time objects we create are actually Sunday or not. Then we printed the results.

RELATED TAGS

ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring