How to Display the Current Date in "Mm/Dd/Yyyy" Format in Rails

How to display the current date in mm/dd/yyyy format in Rails


<%= Time.now.strftime("%m/%d/%Y") %>

How do I get the current Date/time in DD/MM/YYYY HH:MM format?

The formatting can be done like this (I assumed you meant HH:MM instead of HH:SS, but it's easy to change):

Time.now.strftime("%d/%m/%Y %H:%M")
#=> "14/09/2011 14:09"

Updated for the shifting:

d = DateTime.now
d.strftime("%d/%m/%Y %H:%M")
#=> "11/06/2017 18:11"
d.next_month.strftime("%d/%m/%Y %H:%M")
#=> "11/07/2017 18:11"

You need to require 'date' for this btw.

How to change date format from mm/dd/yyyy to dd/mm/yyyy Ruby on Rails

Two steps:

  • You need to convert your string into Date object. For that, use Date#strptime.
  • You can use Date#strftime to convert the Date object into preferred format.

See implementation below:

str = '01/14/2018'

date = Date.strptime(str, '%m/%d/%Y')
=> #<Date: 2018-01-14 ((2458133j,0s,0n),+0s,2299161j)>

date.strftime('%d-%m-%Y')
=> "14-01-2018"

date.strftime('%Y-%m-%d')
=> "2018-01-14"

Rails Date Format (To match YYYY-MM-DD HH-MM-SS)

Since this is a common thing to want, there is a shorthand method to do this.

DateTime.now.to_s(:db)     #=> 2015-07-31 22:22:05

http://api.rubyonrails.org/classes/DateTime.html#method-i-to_formatted_s

How do I format a date to mm/dd/yyyy in Ruby?

The strftime method can be used to format times:

Time.now.strftime("%m/%d/%Y")

How to display and save MMM-DD-YYYY format with datetimepicker

If you display MM-DD-YY then use below code

<%= t.strftime('%b %d, %Y') %>

For further instruction please see this Or This

How to display date of birth in dd-mm-yyyy format in Ruby

To modify the format of the date of birth in your view, you can use strftime:

<div class="col-md-10 primary-info-label"> 
<label><%= @user.date_of_birth.strftime("%d-%m-%Y") %></label>
</div>

The above should format the date_of_birth to be in mm-dd-yyyy format. More info on strftime here.

Hope it helps!

Set and Display Dates in rails via format: %m/%d/%Y

The best solution I have found is a two-step process:

  1. Allow dates to save within the format of mm/dd/yyyy

    Simply add in the american_date gem. And with that: when you enter into a text_field for a date the format of: mm/dd/yyyy (ex: 1/30/2016) it will save the correct date! This works wonderfully with the bootstrap-datepicker-rails gem because when you use the datepicker to pick a date: it loads into the textbox the date in this format.

  2. Display dates in the format of mm/dd/yyyy

    however, even with the american_date gem installed: rails still wants to display the date in the yyyy-mm-dd format.

    Example: within a text_field, such as: <%= f.text_field :date %>, when the user goes back to edit that date, it will redisplay the date in the format of yyyy-mm-dd which is not what we want. We want rails to redisplay the date in the mm/dd/yyyy format.

    Another Example: if you have an erb tag, such as <%= @blog.some_date %> then yet again: rails will display the date in the format of yyyy-mm-dd. Again: this is not what we want. We want that erb tag to automatically redisplay the date in the format of mm/dd/yyyy

    To fix this: you have two options:

    One option is to open up your config/locals/en.yml file and use the setup specified in the original question of this post. I personally do not like this setup because you then need to remember to use the l method everytime you want to display a date within your app.

    The other option (which I like best) is to go to your config/initializers directory and make a new file. Name the file anything you want. I named the file date_display_format.rb. Then within there just put this line: Date::DATE_FORMATS[:default] = "%m/%d/%Y". Now: a date within your erb tags will display in the proper format, and when you go to edit your date attribute within a form: with the text box the date will display in the proper format as well.



Related Topics



Leave a reply



Submit