Heads up: This description was created by AI and might not be 100% accurate.
monday_of_this_week2.rb
This Ruby code snippet demonstrates how to get the previous Monday from a given date. The code first gets the current time using Time.now
, then calculates the number of seconds since the previous Monday by subtracting the number of seconds in one day multiplied by the weekday difference between the current date and Monday (which is 1).
The resulting integer represents the number of seconds from the previous Monday to the current date, which can be used to create a new Time
object with the desired date.
Ruby code snippet
today = Time.now
#=> 2025-06-27 15:34:17.472915966 +0000
this_monday = today - (today.wday - 1) * 24 * 60 * 60
#=> 2025-06-23 15:34:17.472915966 +0000
Executed with Ruby 3.4.4
.