Formatting a date object to display a human readable date - ruby-on-rails

Here's what I'd like to display:
May 13, 2012
Here's what is being displayed:
2012-05-13
I searched for some answers and it led me to "Formatting Dates and Floats in Ruby", where it mentions a possible solution:
<p class="date"><%= #news_item.postdate.to_s("%B %d, %Y") %></p>
However this doesn't change the output at all. No debugging errors, or exceptions are fired.
I can do this and it works perfectly fine:
<p class="date"><%= Time.now.to_s("%B %d, %Y") %></p>
Here is my migration file (to see what data type I used):
class CreateNewsItems < ActiveRecord::Migration
def change
create_table :news_items do |t|
t.date :postdate
t.timestamps
end
end
end

Date.to_s is not the same as Time.to_s. Your postdate is a Date, so therefore you might want to look at strftime instead:
postdate.strftime("%B %d, %Y")
Or even look to add your own custom date format to your Rails app:
Need small help in converting date format in ruby

The to_formatted_s function already has some common human readable formats for DateTime objects in Rails.
datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
datetime.to_formatted_s(:short) # => "04 Dec 00:00"
datetime.to_formatted_s(:long) # => "December 04, 2007 00:00"
datetime.to_formatted_s(:long_ordinal) # => "December 4th, 2007 00:00"
datetime.to_formatted_s(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
datetime.to_formatted_s(:iso8601) # => "2007-12-04T00:00:00+00:00"

<%= time_ago_in_words #user.created_at %>
result: 9 hours ago

To convert created_at time to a human-readable format, follow the below steps:
First, convert it to local time like(UTC to local time)
time_stmap = #user.created_at.localtime
For the time
time_stmap.strftime("%I:%M %p")
For the date
time_stmap.strftime("%B %d, %Y")

Related

How to add the characters "st" ,"nd", "th" next to the day e.g 1st, 2nd, 28th

I am trying to setup a date format but from docs and other websites i checked there is no mention on how to make this type of format
2nd of January 2017
Date.tomorrow.strftime("%e %B %Y")
will give
28 January 2017
how can i make it
28th of January 2017 ?
is it possible?
If you want the exact format you mentioned :
d = Date.tomorrow
d.strftime("#{d.day.ordinalize} of %B %Y")
=> "28th of January 2017"
If you use it multiple times, you could define :
Date::DATE_FORMATS[:my_date_format] = lambda { |date| date.strftime("#{date.day.ordinalize} of %B %Y") }
in config/initializers/date_formats.rb (create it if not already present)
You can then call :
Date.tomorrow.to_s(:my_date_format)
=> "28th of January 2017"
In Rails Time, Date and DateTime have to_formatted_s methods:
In your case you're looking for Date#to_formatted_s:
Date.tomorrow.to_formatted_s(:long_ordinal)
#=> "January 28th, 2017"

Displaying weekday on Active Admin column

How it's possible to display the weekday next to a date ?
column :start_date #displaying for example 29 Jan. 2016
How to echo it as Friday, 29 Jan. 2016?
You can use strftime method.
column "Start Date:" do |post|
post.start_date.strftime("%A, %d %b. %Y")
end

Ordinalize in embedded ruby

Is there a quick way to be able to ordinalize the following code?
<%= time_tag(Date.today, :format=>'%A %d %b') %>
The current output reads
Tuesday 18 Feb
I want to ordinalize the date to show
Tuesday 18th Feb
Any suggestions?
You can use Date::DATE_FORMATS to add a new customized format, and Integer.ordinalize to get the day ordinal:
Date::DATE_FORMATS[:month_ordinal] = lambda { |date|
date.strftime("%A #{date.day.ordinalize}, %B")
}
>> Date.today.to_formatted_s(:month_ordinal)
=> "Tuesday 18th, Feb"
Write as below using #ordinalize :
<%= time_tag(Date.today, :format=>"%A #{Date.today.day.ordinalize} %b") %>

How to parse time field into hours, minutes and seconds?

How would I parse my start_at column to be three different fields for hours, minutes and seconds instead of just one?
Note: I want to keep it as one column and not make three different ones.
Here is my code:
Table:
class CreateTimers < ActiveRecord::Migration
def change
create_table :timers do |t|
t.time :start_at
t.timestamps
end
end
end
Form:
<%= form_for(#timer) do |f| %>
<div class="form-inputs">
<%= f.text_field :start_at %>
</div>
<div class="form-actions">
<%= f.submit 'Start', :class => 'btn-primary span3' %>
</div>
<% end %>
If you just want to split time to three separate inputs you can use the time_select helper.
Otherwise use the strftime method; Check http://strfti.me for more help.
You don't say what format your time values are in. Because users love to be different, if you give them a free-form text field, odds are really good you'll get data in varying formats, so you'll have to be flexible.
You can try using DateTime's parse method, which provides some flexibility for the formats.
DateTime.parse('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('20010203T040506+0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>
DateTime.parse('3rd Feb 2001 04:05:06 PM')
#=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>
There's also the Chronic gem, which allows even more flexibility in how the values can be entered.
Once you have a DateTime or Time value, you can use that object's methods to get at the hour, minute and second fields:
now = Time.now # => 2012-10-03 07:50:26 -0700
now.hour # => 7
now.min # => 50
now.sec # => 26
or:
require 'date'
now = DateTime.now # => #<DateTime: 2012-10-03T07:52:53-07:00 ((2456204j,53573s,622304000n),-25200s,2299161j)>
now.hour # => 7
now.min # => 52
now.sec # => 53
Check out strftime at http://www.ruby-doc.org/core-1.9.3/Time.html
For e.g.
start_at - 2011-04-26 04:14:56 UTC
For hours:
start_at.strftime("%H") = "04"
For minutes:
start_at.strftime("%M") = "14"
For seconds:
start_at.strftime("%S") = "26"
start_at.strftime("%H %M %S")
More detailed documentation is at: http://www.ruby-doc.org/core-1.9.3/Time.html

Ruby on Rails: formatting date inside a field

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.

Resources