In my user edit view I have the following select: <%= f.time_zone_select :timezone %>
So that users can save their own timezone to tailor the dates on our app.
However we've noticed that during DST which is the case at the time of this posting the select is still showing (GMT+00:00) London instead of UTC+00:01 or BST.
Is this an option in Rails to show the correct timezone? Or is what is currently being show actually correct instead?
I have the following method
<%= message.created_at.strftime("%H:%M") %>
that currently returns 21:00
I need to display a Europe/Kiev time zone which is 3 hours ahead than usual Greenwich's time zone. How do I set it in the method input?
You can use a beautiful gem named "local_time". It takes care of local rendering of the time in your views. It has very nice documentation as well. Visit https://github.com/basecamp/local_time
If you stored time in your database in UTC than that time will always be in UTC when you fetch it. You need to display it according to user's local time zone. You have to do that using JavaScript, check this article.
Or if you need just Kiev time zone and nothing else you can do something like this:
<%= message.created_at.in_time_zone('Europe/Kiev').strftime("%H:%M") %>
I have a Rails app on PGSQL and there is a Date of Birth column (timestamp without time zone).
I use a "select" to get the employee data back and return that as JSON, but the DOB always comes back with a timezone, e.g. "dob":"1992-06-18T20:00:00-04:00".
I tried using to_char(emp.dob, 'YYYY-MM-DD HH24:MI:SS'), but no effect.
Any one has any ideas how to get the o/p to not contain TZ information?
Something like: 1992-06-18 20:00:00
Thanks
EDIT: Found the solution to a similar question here:
rails dates with json
Use the localization method and type the following:
<%= l emp.dob, :format => :long %>
There are several pre-defined options to show the text (:long, :short etc)
You can read more here.
I am building a simple event system and i am having problem with rails time helper conversions. I want to be able to have a 12hour clock that has an output of something like 12:00am or 12:00pm using something like <%= event.start_at %> to give the time output in the 12hour clock format
If event.start_at is already a date or time object (e.g. an ActiveRecord instance) then you could do something like:
<%= event.start_at.to_s(:my_format) %>
You'd need to define :my_format in an initializer:
# config/initializers/date_and_time_formats.rb
Time::DATE_FORMATS[:my_format] = '%I:%M%P' # 12:30pm
You can tweak the format to your liking and use it over and over again using to_s(:my_format). #Dave Newton pointed out where to go for formatting above.
http://apidock.com/ruby/Time/strftime
I'm working with an app for a concert tour website, where all times (announcement times, on-sale start times, and event start times) are local to each particular venue's time zone. I take the user entered date/time where applicable and run a before_filter to set the appropriate time zone so that everything is stored in the database in UTC. For the 'new' form and for displaying the times in index and show actions, no problem at all. When the data is brought back out of the database and into a view, I use in_time_zone to adjust per the particular venue.
The only issue is on the edit form. The date/time select is showing the data in UTC. When I'm working on the site, I mentally adjust, but for others it's confusing. I'd like to do something along the lines of:
<%= f.datetime_select :start_datetime.in_time_zone(#event.time_zone) %>
Or, in the controller:
def edit
#performance = Performance.find(params[:id])
#event = #performance.event
#performance.start_datetime = #performance.start_datetime.in_time_zone(#event.time_zone)
end
Then simply, <%= f.datetime_select :start_datetime %>.
Unfortunately, I haven't found the right way to do this. Do you have any ideas worth giving a shot?
Thank you very much.
You may use default method of datetime_select, as following:
%br
= f.label :req_sess_start, "Session starts at"
= f.datetime_select(:req_sess_start, :start_year => 2010, :ampm => true, :default => 0.days.from_now.in_time_zone(#timezone))
Because of the default value shown, client will assume that times has to be entered in his/her local timezone, but...
this value will be actually in the timezone default to your application (as provided in application.rb, and the default is UTC).
So, you would require some server side coding to convert it to correct value.
I'm not sure if I understand what you want to do, but since you're storing the #event.time_zone, you could add
:default => start_time.in_time_zone(#event.time_zone)
to your datetime_select form field.
How about something like this:
# change PST to correct zone abbrev.
new_zone = ActiveSupport::TimeZone.new("PST")
#performance.start_datetime = #performance.start_datetime.in_time_zone(new_zone)
I just noticed this is an old post but:
If I were you, I would use a virtual attribute to represent the date time of the venue. For instance, you could add an attribute to performance called adjusted_start_time.