Rails Convert yyyy-MM-dd'T'HH:mm:ssZ To date - ruby-on-rails

Following is my query where I am fetching order wise payments on latest first basis.
I wish to fetch OrderPayment created_at here.
If I write <%= OrderPayment.last.created_at%> I get 2019-06-28 16:31:12 +053 which is a date, and I'm currently using <%= order_payment["created_at"]%> where I get 2019-06-28T11:01:12.686086 which is a string.
Why am I getting such a weird representation?
#order_payments = OrderPayment.select("order_id, json_agg(order_payments.* order by order_payments.created_at desc) as details").includes(order:).order("order_id desc").group(:order_id)
#iterating #orderpayments for listing
<% #order_payments.each do |order_detail| %>
<%details = order_detail.details%>
<%=order_payment["created_at"]%>
<%end%>
if i do <%= Time.at(DateTime.parse(order_payment["created_at"])).strftime("%d-%m-%y %I:%M %p")%> i get output like OrderPayment.last.created_at but i want to know whats the correct way to do it?

I think in your situation database and your rail environment time zones are different.
and its returning db value as it is with timezone
you should try Time.zone.parse(//your query//)
It will return time and date with current time zone.

Related

Rails timezone select doesn't show DST

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?

How to correctly set time format in strftime method?

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") %>

Rails - Don't want timezone in sql o/p

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.

Simple time helper in rails

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

Rails - Date time select in specified time zone

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.

Resources