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.
Related
So, I have a rails application which there is a datepicker. When I put the date for the first time in the calendar it appears in this format: dd-mm-yyyy.
But rails save in the database in this format: yyyy-mm-dd.
When I go to edit this in the form, the datepicker get crazy, it appears a 1900 year, not the actual year.
I know this happens because the format date is different(the date which bootstrap expects and the date which is saved).
I tried change the datepicker format in the edit view(.html.erb) but it doesn't work. How can I solve this?
<%= f.text_field :my_date, "data-provide" => 'datepicker',
'data-date-format' => 'dd-mm-yyyy', class: 'datepicker form-control', :required => true %>
I am using this guide, http://blog.nicoschuele.com/posts/cheatsheet-to-set-app-wide-date-and-time-formats-in-rails, and it doesn't seem to be working.
Currently, whenever I make a JSON request and get back data my created_at and other time fields show iso8601 format.
An example of the format is 2014-10-23T00:35:14.800Z. I would prefer something more readable like Sun, 3 Nov 2013 14:22:18 -0700
I did what the guide above said to do, but when I restart my server and fire off the request again, the JSON still returns the data in iso8601 format.
How do I remedy this?
I don't recommend it but you can set it Rails wide as you've requested at this link: http://blog.nicoschuele.com/posts/cheatsheet-to-set-app-wide-date-and-time-formats-in-rails
I'll give you what I used for my html DateTime conversion. I do not have a JSON answer, but I hope this'll give you the answer you're looking for. I agree with Nils Landt in
You do not want to change the time format in the API. Keep it as ISO 8601, and change it on the client side. Way less hassle all around.
Here's what I put in my controller:
before_action :correct_datetime, only: [:create, :update]
def correct_datetime
params['event']['starts_at'] = DateTime.strptime(params['event']['starts_at'],'%m/%d/%Y %l:%M %p') unless params['event'].try {|i| if i.try(:has_key?, 'starts_at'); i['starts_at'].empty?; else; true; end }
params['event']['ends_at'] = DateTime.strptime(params['event']['ends_at'],'%m/%d/%Y %l:%M %p') unless params['event'].try {|i| if i.try(:has_key?, 'ends_at'); i['ends_at'].empty?; else; true; end }
end
And in my view I'm using Bootstrap DateTimePicker for some text areas.
Starts at: <%= f.text_field :starts_at, value: (f.object.starts_at.strftime('%m/%d/%Y %l:%M %p') if f.object.starts_at), class: 'form-control', style: 'width:300px;' %>
Ends at: <%= f.text_field :ends_at, value: (f.object.ends_at.strftime('%m/%d/%Y %l:%M %p') if f.object.ends_at), class: 'form-control', style: 'width:300px;' %>
To allow the datetime picker to work I use the following in my event.js.coffee CoffeeScript file:
$ ->
$("#event_starts_at").datetimepicker()
$("#event_ends_at").datetimepicker()
return
I apologize that I don't have the JSON results for this. But I have given you the input and output formatting methods for accomplishing what you seek.
To convert from a DateTime object from the DB you use strftime. And to format it back to save in the DB you use strptime.
I have a datetime that gets rendered into a hidden form field using simple_form. The field is called next_event: The erb line looks like this:
<%= f.input :next_event, as: :hidden, label: false %>
I want this string to be the datetime in the user's local time zone. If I set a breakpoint in the controller code, I can see that the variable is in the correct time zone (in this case, MDT):
:next_event => Wed, 28 May 2014 15:30:00 MDT -06:00,
However, when it gets rendered in HTML, the date time is converted to UTC:
<input class="hidden" id="foo_next_event" name="foo[next_event]" type="hidden" value="2014-05-28 21:30:00">
What can I do to get simple_form to stop converting to utc?
You can manually specify the value so that the TimeZone conversion doesn't take place:
<%= f.input :next_event, as: :hidden, label: false, input_html: { value: f.object.next_event.to_s } %>
Another option... may be to use the *_before_type_cast method for your attribute. So in the model define:
def next_event_before_type_cast
self[:next_event].to_s
end
With this approach you should get the result you want without having to worry about formatting of the date in forms here (or elsewhere).
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.
I'd like my dates in the mm/dd/year format in text fields. However, they currently displays as 2010-03-26.
Is there a global setting I can set to change this?
I tried the following, which seems to update the .to_s method, but form fields stay the same.
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(:default => '%m/%d/%Y')
Thanks
You have to register the default format in an initializer.
Add this line to the config/initializers/date_time_formats.rb.
Date::DATE_FORMATS[:default] = '%m/%d/%Y'
# if you want to change the format of Time display then add the line below
Time::DATE_FORMATS[:default]= '%m/%d/%Y %H:%M:%S'
# if you want to change the DB date format.
Time::DATE_FORMATS[:db]= '%m/%d/%Y %H:%M:%S'
Now in the script\console lets test the format.
>> Date.today.to_s
=> "03/14/2010"
>> Time.now.to_s
=> "03/14/2010 13:20:55"
I don't know if there is a global setting for that anywhere, I just do it in the ERB.
<%= text_field_tag("air_date_date", air_date.blank? ? "" : air_date.strftime("%m/%d/%Y"), :class => "date-input text") %>
Alternatively, you can factor this out into a helper function to make it DRY.
I know this is an awfully old question, but you could use date_field instead of text_field. Perhaps that wasn't an option when you asked this question originally.
It displays the date in mm/dd/yyyy, which is your intent.
<%= date_field :column_name %>
The date_select form helper provides a "bare bones" date selector.