J.Luca
J.Luca

Reputation: 208

How to check if today is monday?

To know the year, I use <%= Time.now.year %>.

So I can check the year using <% if Time.now.year == 2020 %>You are living the 2020!<% end %>.

But... how to check if today is Monday? Or Sunday?

Upvotes: 1

Views: 1582

Answers (3)

thutt
thutt

Reputation: 650

> Time.now.monday?
=> false
> 2.days.ago.monday?
=> true

Upvotes: 3

cdadityang
cdadityang

Reputation: 543

You can use:

Time.now.wday
# => 3

# this means it's wednesday

wday stands for Week day. And 0 stands for sunday and 6 stands for saturday.

So for monday you can check: Time.now.wday == 1, for sunday you can check: Time.now.wday == 0

Upvotes: 1

lokhi
lokhi

Reputation: 177

you can use .monday? on Date.today

https://ruby-doc.org/stdlib-2.6.1/libdoc/date/rdoc/Date.html#method-i-monday-3F

And you can use .year if you need to compare to 2020 :)

Upvotes: 1

Related Questions