Format the Date Using Ruby on Rails

Rails formatting date

Use

Model.created_at.strftime("%FT%T")

where,

%F - The ISO 8601 date format (%Y-%m-%d)
%T - 24-hour time (%H:%M:%S)

Following are some of the frequently used useful list of Date and Time formats that you could specify in strftime method:

Date (Year, Month, Day):
%Y - Year with century (can be negative, 4 digits at least)
-0001, 0000, 1995, 2009, 14292, etc.
%C - year / 100 (round down. 20 in 2009)
%y - year % 100 (00..99)

%m - Month of the year, zero-padded (01..12)
%_m blank-padded ( 1..12)
%-m no-padded (1..12)
%B - The full month name (``January'')
%^B uppercased (``JANUARY'')
%b - The abbreviated month name (``Jan'')
%^b uppercased (``JAN'')
%h - Equivalent to %b

%d - Day of the month, zero-padded (01..31)
%-d no-padded (1..31)
%e - Day of the month, blank-padded ( 1..31)

%j - Day of the year (001..366)

Time (Hour, Minute, Second, Subsecond):
%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')

%M - Minute of the hour (00..59)

%S - Second of the minute (00..59)

%L - Millisecond of the second (000..999)
%N - Fractional seconds digits, default is 9 digits (nanosecond)
%3N millisecond (3 digits)
%6N microsecond (6 digits)
%9N nanosecond (9 digits)
%12N picosecond (12 digits)

For the complete list of formats for strftime method please visit APIDock

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

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.

Formatting Time and Date in Ruby on Rails

Looks like the problem is just a stray l at the beginning of your code.

This should work:

<%= post.created_at.strftime("Posted on %B %d, %Y at %H:%M") %>

Better yet would be to move this time formatting to a model and call that on post. And move "Posted on" outside the method. So:

<td> Posted on <%= post.formatted_created_on %> </td>

Formatting Date/Time in Ruby to YYYY-MM-DD HH:MM:SS

You can use the Time#strftime method to format the time into a variety of different formats, including the one which you need. It takes a parameter which specifies the format of the date output. Look at the documentation for this method for more instructions about how to use it.

Time.now.strftime("%F %T")

The %F specifies that we would like the date in YYYY-MM-DD format. This is followed by a space, then the %T specifier, which produces a HH:MM:SS time output.

To incorporate this into your code:

"erstellzeit": "#{Time.now.strftime("%F %T")}"

Alternatively, if you're getting an additional +0000 output, try:

"erstellzeit": "#{Time.now.strftime("%F %H:%M:%S")}"

How do I format datetime in rails?

Use ruby's strftime() on dates/datetimes:

<%= link_to timeslot.opening.strftime("%Y %m %d"), [@place, timeslot] %>

Have a look at the documentation to find out how the formatting works.

Formatting Date in Rails


In Rails 4 or above

> Date.today.beginning_of_month + 14
#=> Sun, 15 Oct 2017
# formatted as per your requirement
> (Date.today.beginning_of_month + 14).strftime("%B %d, %Y")
#=> "October 15, 2017"

Date#beginning_of_month it will return you beginning date of month of specified date (which will be always 1st, add 14 days) so you will get 15th of that month



Related Topics



Leave a reply



Submit