Build a form with date_select for dates in range

Hello,

I was wondering if it’s possible to generate a list of select values for a given range.

Let’s say I have 2 dates, ~D[2019-01-01] and ~D[2020-02-02] and I’d like to generate a select using date_select helper with values only in that range. I could probably deconstruct these dates into years, months and days, but if feels like it’s not the most straightforward way of achieving my end goal.

What would be a recommended way of doing something like this?

Thanks!

So you want a select option for each date between these two?

Yes. I guess I could just use a select helper and pass a list of dates as its 3rd argument. Does this sound like a reasonable approach?

Yes, and in case to create the list of dates from the range start and end you can use Date.range/2, which returns a DateRange enumerable:

Date.range(~D[2019-01-01], ~D[2020-02-02])
#=> #DateRange<~D[2019-01-01], ~D[2020-02-02]>
1 Like

Thank you, that works.

1 Like