Change Date format for generated scaffold? - ruby-on-rails

I want the date to be day, month, year. i.e. Jan 1, 2015. How can I change the format to this and what file should I put it in?
I want the date to show up in this format within index.html.erb and in the _form.html.erb.
I accidentally did a scaffold as datetime, but manually changed everything back to date, then ran rake db:reset. Now when I create something the date comes up as: 2014-12-31 00:00:00 UTC.
Thanks for any help you could give me!

Use strftime to format your dates into formatted strings.
DateTime.now.strftime("%d/%m/%y")
Edit
In a rails view
<%= DateTime.now.strftime("%d/%m/%y") %>

Related

Treat a created_at date as a date instead of a time for formatting

In my rails app, I have the following configuration for date formats:
config/initializers/time_formats.rb
Date::DATE_FORMATS[:concise] = "%d/%b/%Y"
Time::DATE_FORMATS[:concise] = "%d/%b/%Y %H:%M %Z"
In my view, if I do:
MODEL.created_at.to_formatted_s(:concise)
... it correctly picks-up the :concise format for the datetime date type, and renders it as follows:
13/Oct/2020 05:00 UTC
Note that the %b is the short Month name - Oct in this case. If I only want to show the date, I would assume that I'd simply use the to_date method to convert the datetime to a date first:
MODEL.created_at.to_date.to_formatted_s(:concise)
This does render a date, but it doesn't pick up the :concise date format properly. It renders:
13/10/2020
Note it's not rendering October as 10, instead of formatting as %b. I've tried adding a :default date type in time_formats.rb but that didn't fix it.
Why is this happening just for the Date type?
(I'm using ruby 2.5.8, Rails 6.)

Ruby on Rails showing truncated date for each date

I am using RoR with MSSQL server as database, in a table I am saving date with 'date' datatype.
RoR saving the date correctly e.g if I am saving 2012-10-20 then it is saving 2012-10-20 but when I display it on front-hand it display like 2012-10-02 for 2013-10-20, 2012-10-01 for 2013-10-10, 2012-10-01 for 2013-10-15 etc.
What is going wrong... I can't figure it out
Edit
I am not asking for date formating technique. The issue is that date not fetched correctly as it saved in database, if I used datatime datatype instead of date datatype then all goes right.
You should do:
<%= #user.created_at.strftime("%Y-%m-%d") %>
Or if you want it to be more readable then:
<%= #user.created_at.to_date %>
You can try both which ever you like
EDIT
You have to do like mentioned above otherwise you have to create a new attribute to store date only. created_at is default timestamp type See this link. You can use any of both ways either convert created_at to date or introduce new column to table
How are you displaying it on your view?
For example the following would show "January 2010"
<%= #user.created_at.strftime("%B %Y") %>
You probably want year, month, date? So %Y %m %d
According to the github page of the adapter:
Date/Time Data Type Hinting
SQL Server 2005 does not include a native data type for just date or
time, it only has datetime. To pass the ActiveRecord tests we
implemented two simple class methods that can teach your models to
coerce column information to be cast correctly. Simply pass a list of
symbols to either the coerce_sqlserver_date or coerce_sqlserver_time
methods that correspond to 'datetime' columns that need to be cast
correctly.
class Topic < ActiveRecord::Base
coerce_sqlserver_date :last_read
coerce_sqlserver_time :bonus_time
end
This should point you in the right direction

How to display local time on form when column has type datetime is saved in database in UTC

I have a column called test_date, type datetime in General Exam table. I built a form to create and update GeneralExam, on form, I allow User choose a date and time throught jQuery UI Datepicker.
After user choose date and time, it will displayed in textfield, like:
25-11-2012 16:30
It is my current time. When I run in console, I see the test_date was saved in database is:
2012-11-25 09:30:00
And when I get object GeneralExam, and get test_date value, it is:
g = GeneralExam.last
g.test_date
=> Sun, 25 Nov 2012 16:30:00 ICT +07:00
I think test_date is converted, because I set up in application.rb is:
config.time_zone = 'Hanoi'
But when I update a General Exam on form, it still display the time was saved in database, not my local timezone, it means it display:
2012-11-25 09:30:00
But I want it is like what I chose on form:
25-11-2012 16:30:00
How can I set up for display datetime column in local time?
And I think the order of date was reversed, when I choose date, it is 25-11-2012, but when it displayed it was 2012-11-25, Why it reversed the order?
After few hours I search on google and stackoverflow, I think I found what I want. I have looked at this question and find some information about strftime method. This is what information I need to solve my problem.
First, How to display datetime was saved in UTC as local time when display it:
Value was saved: 2012-11-25 09:30:00
Value I want display on form (ICT +7:00): 25-11-2012 16:30:00
To display value I want, I used this on form (I used simple_form):
<%= f.input :test_date, as: :string, label: false, input_html: { id: "datetime_picker", value: f.object.test_date.strftime("%d-%m-%Y, %H:%M") } %>
I have got value in database and format it by:
value: f.object.test_date.strftime("%d-%m-%Y, %H:%M") } %>
And when I want display test_date in table data, I also used strftime("%d-%m-%Y, %H:%M") to display datetime format as I want.
The proper setting is related with your Rails config , but also with your DB config . There is a Railscast , explaining how to avoid most of the gotchas . For consistency all the dates and times are saved in the database in UTC . The conversion is made by Rails config , depending on your config.time_zone . In this case you should try :
Time.zone.now
which takes in account the configuration in Rails and is different from:
Time.now

Rails convert date format

I have a form input in my application that accepts dates in the format DD/MM/YYYY eg 02/05/2012 is 2nd May 2012.
What is the best way for me to convert this into a suitable format to be added to the database through ActiveRecord?
Is there a simply way of converting 02/05/2012 to 05/02/2012 before adding to the database?
This is similar to Getting rails to accept European date format (dd/mm/yyyy)
In your model, create a setter method, where "my_date" is your database field.
def my_date=(val)
Date.strptime(val, "%d/%m/%Y") if val.present?
end
For this specific format you can call DateTime.parse:
DateTime.parse("02/05/2012") # => Thu, 02 May 2012 00:00:00 +0000
2 Years later but it's something I came across too, here's my fix.
In your view, use:
<%= l article.created_at, format: :euro %> **make sure to add the l
And then in /config/locales/en.yml add:
en:
time:
formats:
euro: "%d/%m/%Y"
** you can also just add, euro: "%D" to display in the us date format.
Thanks.

In Rails, how can I display a date such as 2009-12-01 as Monday, December 1st, 2009

I have a birthday field in my database which has a date value as yyyy-mm-dd. I want to display it as being more human friendly. So 2011-11-15 should show up as Tuesday, November 15th, 2011.
The very strange thing is that when i do a #user.birthday in rails console, the value does show up in the format I want. But not on the web.
I am not sure what's going on.
The reason it does not show up in the right format is that Rails in the view does call a to_s if necessary.
You should the Internationalization and Localization of Rails to do that.
So in your example, I18n.l #user.birthday should do the trick. You should check what the default date format for your locale is, this is located at `config/locales/.yml. You may add your format by following the explanation in "How to store custom translations". So by adding
en:
date:
formats:
default: "%A, %B %d,%Y"
this format will be used where ever you call I18n.l on a date.
#user.birthday.strftime "%A, %B %d,%Y"
Try this <model>.created_at.to_date.to_s(:long)

Resources