Snoobie
Snoobie

Reputation: 760

How to get the current week from date

I'm actually trying to reproduce this kind of calendar: enter image description here

What I want is simple, I'm trying to get for the second columns a dynamic display for the days. I'm finding a magic function that will return the 7 next day from a specific day. Or if exists an approach to solve this problem ?

In the example we could have this format: ["Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"] because the current day is Tuesday.

But if the current day is Wednesday, I would like to get this format: ["Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue"]

Actually I'm using l(Date.today, format: '%a').titleize to get the current day.

Upvotes: 1

Views: 344

Answers (1)

Ursus
Ursus

Reputation: 30056

Anything like this?

today = Date.today
(today..(today + 7)).map { |d| l(d, format: '%a').titleize }

So

def short_days_of_week_from_date(date)
  (date..(date + 7)).map { |d| l(d, format: '%a').titleize }
end

Upvotes: 2

Related Questions