I use datetime_select in my application and i want to use such order of time and date:
HH MM - DD MM YYYY
But i can't get how to do that. :order option allow only array that containing :day, :month and :year.
Is there any solution to solve that?
:order => [:hour, :minute, :day, :month, :year].
That should do it.
Related
I have a date_of_birth field with type date and I need to correct the following two issues:
The year range is limiting to 2010..2020
The order of the fields is YYYY - MM - DD and I would like it to be MM - DD - YYYY
I was able to get the desired output as follows (strictly with simple_form):
<%= f.input(:date_of_birth, start_year: 1900, end_year: Time.now.year - 1, order: [:month, :day, :year]) %>
I currently have one text field for a date entry, I am trying to split the year, month and day up into three individual entries, seperated by '/'. The original text entry looks like:
<%= f.text_field :date, :placeholder => 'YYYY/MM/DD' %>
I would like to split this into three text_fields, and append them together, and put it into the date entry in the database.
How can I do this?
You often use textfields for dates because you then only needs one field. When you are having three different fields should you consider using select instead. Rails have a the date helper method date_select, so it would be somethink like this:
<%= f.date_select :date %>
This creates one select for years, one for months and one for days.
You can read more on http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select
Add three virtual attributes to the model:
attr_accessor :form_month, :form_day, :form_year
Then put the following in the controller, not the model:
def create
# ...
form_date = [ params[:form][:form_month], \
params[:form][:form_day] ,
params[:form][:form_year] ].join("/")
#my_model.date = Date.parse(form_date)
# ... save and return ...
end
It would be a good idea to manually check each form parameter for validity. Date#parse may spit out the incorrect date if fed an incomplete date string.
Date.parse "2005/11/4"
# => Fri, 04 Nov 2005
Date.parse "/11/4"
# => Mon, 04 Nov 2013
Try to use https://github.com/plataformatec/simple_form
<%= f.input :deadline, :start_year => Date.today.year, :end_year => Date.today.year + 1, :order => [:day, :month, :year] %>
Do you have many options.
I am developing a rails 3.2 app.
I have the following configuration in initializers/time_format.rb file.
Date::DATE_FORMATS.merge!(default: "%m/%d/%Y")
Date::DATE_FORMATS.merge!(db: "%m/%d/%Y")
Time::DATE_FORMATS.merge!(default: "%m/%d/%Y %H:%M:%S")
Time::DATE_FORMATS.merge!(db: "%m/%d/%Y %H:%M:%S")
I want all date format %m/%d/%Y.
It works fine except that input element shows "yyyy-mm-dd" format?
What am I missing?
Thanks.
Sam
Are you talking about the date_select form helper?
Maybe try modifying your form like this?
date_select("article", "written_on", :order => [:month, :day, :year])
Check the API for more details.
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.
I am doing a validations in my model to check if a date is older than today (meaning a date can only be in future from the current time)
I was about to write down a block of code, but was wondering is there a build in function for this.
The date is passed from a view using:
<%= date_select ('load', :valid_until, :order => [:day, :month, :year]) %>
All ideas are appreciated.
Yep, there's indeed a built-in date.future? method.