Ruby: How to store and display a day of the week?
Ruby's Date
class already has a constant defined that holds the name of the days in an array: Date::DAYNAMES
.
For example, you could do something like:
days = []
Date::DAYNAMES.each_with_index { |x, i| days << [x, i] }
I don't know if I'd necessarily say that this is better, but you can definitely leverage what is already available to you.
How do I store the day of the week in rails?
There are certainly a number of ways to do this. If you are always going to be storing consecutive days, then maybe two separate database columns representing the start and end dates(index of 1-7) would be in order (with a method to translate from day index to textual name).
This may work for your situation, however, I find that more often you will need the flexibility of inserting mulitiple non-consecutive days. One method that I have used recently (in an effort to minimize SQL queries), is to simply add 7 boolean database fields to your table representing Sun-Mon. This would allow you to use methods such as @event.monday? A helper method could lump consecutive days together for display purposes.
How to get day of the week next week?
Your syntax for next_week
needs tweaking:
# Works
<%= Date.current.next_week(:tuesday) %>
To understand why your original approach doesn't work: monday
and sunday
just happen to be defined on DateAndTime::Calculations, but the other days of the week are not.
How can I display the days of the week?
You can use a range and iterate through:
<% (Date.today.beginning_of_week(:sunday)...Date.today.end_of_week).each do |date| %>
<p><%= date.strftime('%a %d') %></p>
<% end %>
Will give you:
<p>Sun 28</p>
<p>Mon 29</p>
<p>Tue 30</p>
<p>Wed 01</p>
<p>Thu 02</p>
<p>Fri 03</p>
<p>Sat 04</p>
Note that we're using ...
instead of ..
for the range.
How can I output the day of week with embedded ruby?
Say i have date = Time.now.to_date then date.strftime("%A") will print name for the day of the week and to have just the number for the day of the week write date.wday.
Copy from here
So Time.now.strftime("%A")
and in your case
<h6>
<%= Time.now.strftime("%A, %B %d. %Y") %>
</h6>
Easiest to test this is the rails c
. To remove the leading zero on the %d
use %-d
instead.
Store a day of week (mon, tue, wed, ...) in rails db and compare with the day of today
Sounds like you're on the right track. To figure out the current day of the week:
Date.today.wday
Although that considers Sunday 0 and Saturday 6.
How to get the date by day of the week ruby
To get the current day in Rails, you can use:
Date.today
To get the previous Saturday (since today is Monday 3rd July
, that's Saturday 1st July
), you can use:
Date.today.beginning_of_week(:saturday)
Or, if what you actually wanted was the previous week's Saturday (Saturday 24th June
), then you can use:
Date.today.weeks_ago(1).beginning_of_week(:saturday)
Or, if you prefer:
1.week.ago.beginning_of_week(:saturday)
...However, note that the above will return an ActiveSupport::TimeWithZone
object rather than a Date
object - so will behave slightly differently.
Have a read through the rails documentation - in particular, the ActiveSupport
extensions to ruby's Date
class to see what methods are available.
If, for some reason, you needed to do this in pure ruby
(i.e. without the above mentioned ActiveSupport
extensions that come bundled with rails
), then you could instead utilise the Date.parse
method:
require 'date'
Date.parse("Saturday") - 7
# => Sat, 01 Jul 2017
In order to convert your Date
(or similar) object to the string format you desire ("24-June-2017"
), you can use ruby's strftime
method:
(Date.parse("Saturday") - 7).strftime('%d-%B-%Y')
In rails, it is a common convention to place such custom date formats in a locale or initializer to avoid repetition. You can then reference this format by name:
# config/initializers/date_formats.rb
# (Call this whatever you want!)
Date::DATE_FORMATS[:calendar] = '%d-%B-%Y'
# Anywhere else in the rails app
Date.today.beginning_of_week(:saturday).to_s(:calendar)
# => "01-July-2017"
Multiple days of week in one attribute in database
Try this,
<%=select_tag 'days[]', options_for_select(Date::DAYNAMES.zip((0..6).to_a),
[selected days array goes here]
), :multiple => true%>
For using checkbox
<ul>
<% Date::DAYNAMES.zip((0..6).to_a).each do |day| %>
<li>
<%= check_box_tag 'days[]', day[1], [selected days array].include?(day[1]) -%>
<%= h day[0] -%>
</li>
<% end %>
</ul>
Hope it's help you.
display list of days in a week in Rails using Date::DAYNAMES
Replace
<% days = []
Date::DAYNAMES.each_with_index { |x, i| days << [x, i] } %>
<% days.each_with_index do |day,index| %>
<div class="field">
<%= f.check_box day[0] %>
</div>
With
<%= f.label :FIELD_NAME%>
<% Date::DAYNAMES.each do |day| %>
<%= f.check_box :FIELD_NAME, {}, day %>
<%= day %>
<% end %>
How can I calculate the day of the week of a date in ruby?
I have used this because I hated to go to the Date docs to look up the strftime syntax, not finding it there and having to remember it is in the Time docs.
require 'date'
class Date
def dayname
DAYNAMES[self.wday]
end
def abbr_dayname
ABBR_DAYNAMES[self.wday]
end
end
today = Date.today
puts today.dayname
puts today.abbr_dayname
Related Topics
Check If a File Exists Using a Wildcard
How to Run My Ruby Code After Rails Server Start
How to Expect Some (But Not All) Arguments with Rspec Should_Receive
Other Ruby Map Shorthand Notation
Ruby Undefined Method 'Bytesize' for #<Hash:0X2954Fe8>
Generating a Short Uuid String Using Uuidtools in Rails
Certificate Verify Failed in "Gem Install Foundation"
What's the Fastest Way to Check If a Word from One String Is in Another String
How to Assign Multiple Values to a Hash Key
How to Turn a Ruby Method into a Block
Use Global or Constant Variable in Ruby/Rails
Ruby on Rails, Paperclip, Amazon Aws S3 & Heroku
Pg Error Could Not Connect to Server: Connection Refused Is the Server Running on Port 5432
How to Check for Stdin Input in a Ruby Script