Displaying weekday on Active Admin column - ruby-on-rails

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

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"

How to manipulate date_select to show customized months? Rails

I need my date select form field to show users only the future dates (like for eg. today is 7 apr 2016, then it must show all dates beyond 7 apr 2016) and for month field it must only show the current month,year(any)
<%= f.date_select :start_date,class: 'form-control'%>
Year: any
Date: only future dates
Month: the current ongoing month
There are two ways:
First is that, you validate the date before saving it, at model level. If date is note in future don't save and return error.
Second, you can use jQuery Date Picker and use its minDate option: http://api.jqueryui.com/datepicker/#option-minDate
Using date_select you only have the option of restricting the starting year but not month and day. http://apidock.com/rails/v3.2.1/ActionView/Helpers/DateHelper/date_select
<%= f.date_select :start_date, start_year: Date.today.year, class: 'form-control'%>

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") %>

Rails Time Form Helper

I currently have a form that uses select_date Date.today When I view the form I can select options such as January 3, 2012. Although when I post the value, it reads 2013-01-03 00:00:00
In the create action the parameters for the Show's date is as followed:
#show.date = Date.civil(params[:date][:year].to_i,
params[:date][:month].to_i,
params[:date][:day].to_i)
How might I get this to read as it read when I selected the date, aka "January 3, 2013"?
Thanks for your help.
One answer is to call date.strftime passing in the format string "%B %d, %Y" wherever you want to display the date. (You can use "%B %-d, %Y" if you don't want day to be zero-padded):
Show: <%= date.strftime("%B %d, %Y") %>

Formatting a date object to display a human readable date

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")

Resources