How to use srtftime in views - ruby-on-rails

I am getting an error with strftime.
My views look like:
<%=room.date%> which works and yields "2017-07-27".
However, I want to convert this to "July 27, 2017".
<%= Date.parse(room.date).strftime("%B %e, %Y ") %> does not work and causes the following error:
ActionView::Template::Error (no implicit conversion of Date into
String)
My schema is:
t.date "date"
I don't know what I am doing wrong. Thanks!

If your room.date is type of date you shouldn't try to parse it. So I'd suggest first try to use <%= room.date.strftime('%B %e, %Y'). Please comment below if that wont work.

Just to throw a different hat into the ring, you can also use
<%= l room.date, format: :long %>
which, by default, uses "%B %d, %Y" but can be easily changed by adding into your config/locales/en.yml.
en:
date:
formats:
long: "%B %e, %Y"
time:
formats:
long: "%B %e, %Y"
Use date: if it's a Date object or time: if it's DateTime or Time. This is short for I18n.l and looks like it works as far back a rails 3.2.13 at the least (it's hard to tell which rails you're using).
This way, if you ever want to support different locales the dates are all set up for it and, probably more importantly, it moves the date format out of your views, so if you ever decide you want a different format ("%A %B %e, %Y", for instance) you can change it in one place and all the views displaying dates get updated.
Note: If it is a Time or DateTime you might want to use instead
<%= l room.date, format: :date %>
and in config/locales/en.yml
en:
time:
formats:
date: "%B %e, %Y"
so your format: :long still shows the time portion when you want it to

It's asking for a String instead a Date data type, pass it a String, try with:
Date.parse(room.date.to_s).strftime("%B %e, %Y")

When room.date is a date, why do you want to parse it? You could use the Date directly.
Can you try:
<%= room.date.strftime("%B %e, %Y ") %>

Related

DateTime substitution from a string in Ruby/Rails

Imagine that I have this string coming from some external api. The string format is always same including the HTML tags and everything.
"<p>The update time is <strong>Tuesday 04/28/15 08:30 AM PDT</strong>, please disregard the old timing.</p>"
How do I extract the DateTime from the string (Tuesday 04/28/15 08:30 AM PDT) and convert it to EST and then wrap it back to string around the <strong> tags?
If the string is exactly the same every time, I would just gsub out the parts of the string you don't want.
string_from_api.gsub!(/(.*<strong>|<\/strong>.*)/, '')
Then use strptime like so:
date_time = DateTime.strptime(string_from_api, "%A %m/%d/%y %I:%M %p %Z")
(My favorite strftime resource.)
Then, assuming you're using Rails, you can change the timezone with
est_time = date_time.in_time_zone('EST')
Then you just have to put it all back together:
time_formatted = est_time.strftime("%A %m/%d/%y %I:%M %p %Z")
"<p>The update time is <strong>#{time_formatted}</strong></p>"
def convert_time_message(message)
regex = /<strong\>(.*?)\<\/strong>/
time_format = '%a %m/%d/%y %H:%M %p %Z'
parsed_time = DateTime.strptime(message.match(regex)[1], time_format)
converted_time = parsed_time.in_time_zone('EST')
message.gsub(regex, "<strong>#{converted_time.strftime(time_format)}</strong>")
end
convert_time_message("<p>The update time is <strong>Tuesday 04/28/15 08:30 AM PDT</strong>, please disregard the old timing.")
You should be able to use DateTime.strptime to parse the date you've been given and then DateTime.strftime to output it again once you've modified it to your satisfaction. Something like:
s = "<p>The update time is <strong>Tuesday 04/28/15 08:30 AM PDT</strong>, please disregard the old timing."
s.sub(/<strong>(.*)<\/strong>/) do |s|
# Parse the date in between the <strong> tags
in_date = DateTime.strptime($1, "%A %m/%d/%y %I:%M %p %Z")
edt_time = in_date + 3.hours
"<strong>#{edt_time.strftime("%A %m/%d/%y %I:%M %p EDT")}</strong>"
end

YML Date formatting according to user profile settings

Each user of our application can have different format for Date.
I can use something like this Date::DATE_FORMATS[:default] = "%m/%d/%Y" in the application controller for changing by default date formatting.
But i want to change date formats in the en.yml to:
date:
formats:
default: "%Y/%m/%d"
short: "%b %d"
long: "%B %d, %Y"
How can i change default, short and long date formates in yml file on the fly as we can use Date::DATE_FORMATS[:default] = "%m/%d/%Y".
Note: in view i use <%= l Time.now.to_date, :format=>:short%>
Thanks.
You can use in application controller only with different data format.
write in application controller.
Time::DATE_FORMATS.merge!(:data_format => "%d-%B-%Y")
Time::DATE_FORMATS.merge!(:data_format_month => "%m-%d-%Y")
and in view or helper call data_format or data_format_month.
example
created_on.to_s(:data_format) for displaying D-B-Y format
update_on.to_s(:data_format_month ) for displaying M-D-Y format

Rails Time Form Helper

I currently have a form that uses select_date Date.today When I view the form I can select options such as January 3, 2012. Although when I post the value, it reads 2013-01-03 00:00:00
In the create action the parameters for the Show's date is as followed:
#show.date = Date.civil(params[:date][:year].to_i,
params[:date][:month].to_i,
params[:date][:day].to_i)
How might I get this to read as it read when I selected the date, aka "January 3, 2013"?
Thanks for your help.
One answer is to call date.strftime passing in the format string "%B %d, %Y" wherever you want to display the date. (You can use "%B %-d, %Y" if you don't want day to be zero-padded):
Show: <%= date.strftime("%B %d, %Y") %>

How do i give default date format?

i am trying to make my code more dry and neat hence i used following in my "config/enviornment.rb":
Time::DATE_FORMATS[:default] = "%d/%M/%y"
But not saw any change in date format.Then i try "strftime" method but it doesn't suit my code.Please guide me on some better option .Thanks in advance
You can use the I18n for that.
config/locales/en.yml:
en:
date:
formats:
default: "%d/%M/%y"
short: "%b %d"
long: "%B %d, %Y"
Then use I18n.l Time.now (or <%= l Time.now %>in your views).
EDIT: To specify the format, use <%= l Time.now, :format => :short %>

Ruby on Rails: formatting date inside a field

My field:
<%= f.text_field :expires_at, :label => false, :class => "input-field" %>
but I want the date to be kinda like this when the page loads: June, 1st, 1752 9:54:00 pm
How would I do that?
Why are you using a text_field for a datetime? Consider using time_select instead.
If you really want to format a date that way though, just use strftime.
So, in your case, add
:value => #object.expires_at.strftime('%B %d, %Y %H:%M:%S %p')
You can format dates and times using the strftime method.
See Ruby's strftime and then use :value => #date_value
If you want this date format to be used throughout your application, you can set the default format in your environment.rb file:
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => "%a %m/%d/%Y %I:%M%p")
If you do this, every time you display a date, it will be formatted according to the date format string you've provided.

Resources