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.
Related
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.
When I use f.datetime_select in a rails form, it returns a datetime to my controller in UTC, e.g.
2014-06-18T11:00:00+00:00
My local time zone is set to Melbourne (+10) as specified in my application.rb file:
config.time_zone = 'Melbourne'
So when I retrieve datatimes from my database they are automatically converted to Melbourne (+10) timezone, e.g.
2014-06-17 19:00:00 +1000
I want to compare the datetime returned by f.datetime_select with a field in my database. How can I do this?
i.e. how can i change the time zone of the datetime returned by f.datetime select to 'Melbourne' (+10) without changing the actual time? i.e. convert:
2014-06-18T11:00:00+00:00
to
2014-06-18T11:00:00+10:00
All dates stored in database are in UTC time. So when your app get new date from 'params' you basically have 2 options: to save it to the ActiveRecord model, and during that AR will perform all heavylifting of deciding of what timezone was meant.
If you don't want to save data to the model, you have to deal with it yourself. Date select control return just 3 specially formatted strings in params hash.
Let's say I named field in my form 'birthdate'. Controller will get something like:
"<your_model_name>" => {... , "birthdate(3i)" => "<day>", "birthdate(2i)" => "<month>", "birthdate(1i)" => "<year>", ...}
So you could deal with that info something like:
Time.zone.parse("#{ params[:model]['birthdate(3i)'] }-#{ params[:model]['birthdate(2i)'] }-#{ params[:model]['birthdate(1i)'] }")
And yeah I know that it looks ugly, and after some research I surprised that there is no any 'out of the box' solution )
Say for instance I have a datefield or 3 select fields for day month and yea..
How does the final selection of date get sent to the database? or in what format doesn't it get sent?
20110803 ?
2011-08-03 ?
What I want to do is:
Test for a valid date (a date that has been selected that exists)
Test for an invalid date (invalid would be a date that doesn't exist and also be when nothing has been selected)
I know an idea of how to write these tests but not sure what actually gets sent to the database
What actually appears before save and in what format. String? Integer?
Thanks in advance for responses.
The gets sent to the database as the database expects it. Each database and configuration will yield a different format for a date but this will not make any difference to you as the field is configured to be Date and it will also be a Date on Rails, so you should not care about the format.
While testing, there are many ways you can validate, but you will not be using neither integer or string, you're using Date objects. Here's an example of how you could write the first one:
it 'should find a model today' do
#date = Date.today
#model = Model.create!(:date => #date)
Model.first( :conditions => {:date => #date}).should == #model
end
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.
In my view I want to display the current date in "mm/dd/yyyy" format.
<%= Time.now.strftime("%m/%d/%Y") %>
You could simply do (substitute in Time for DateTime if using straight Ruby -- i.e. no Rails):
DateTime.now.strftime('%m/%d/%Y')
If you're using that format a lot, you might want to create a date_time_formats initializer in your RAILS_ROOT/config/initializers folder (assuming Rails 2), something like this:
# File: date_time_formats.rb
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:human => "%m/%d/%Y"
)
Which will then let you use the more friendly version of DateTime.now.to_s(:human) in your code.
I think you can use .strftime:
t = Time.now()
t.strftime("The date is %m/%d/%y")
This should give "The date is 09/09/10". See here for a great list of the format codes for .strftime.
If you don't need the full year you could try
Time.now.strftime('%D')