How would you convert a day string (i.e. "Monday" or "Wednesday"), into the corresponding wday integer (1 or 3)?
I've come up with this convoluted way
Date.today.beginning_of_week("Monday".downcase.to_sym).wday
You can parse it using strptime:
Date.strptime('Monday', '%A').wday
#=> 1
Date.strptime('Wednesday', '%A').wday
#=> 3
The intermediate date object refers to the weekday in the current week:
Date.today
#=> #<Date: 2018-11-20 ...>
Date.strptime('Monday', '%A')
#=> #<Date: 2018-11-19 ...>
You can also use _strptime (prefixed with an underscore) to extract the date elements which happen to be :wday for a single weekday:
Date._strptime('Monday', '%A')
#=> {:wday=>1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With