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") %>
Related
Question: How can I display the count for how many "orders" there are on a group_by basis (day, week, month)?
I am using the groupdate gem and can't see how I can take out the date information to only display the count or sum. I need the sum of all of the counts added up.
Controller
#clients = User.all.where(affiliate_id: current_affiliate)
#orders_e = Order.all.where(seller: #clients ).where(order_status: [2] )
....
#orders_week = #orders_e.all.group_by { |week| week.created_at.beginning_of_week }
....
#orders_month_2 = #orders_e.all.group_by_month(:created_at)
#orders_day_2 = #orders_h.all.group_by_day(:created_at, last: 1)
#orders_week_2 = #orders_h.all.group_by_day(:created_at, last: 7)
....
#orders_week_count = #orders_week.values.sum
View:
<%= #orders_month_2.count.sum() %>
Output: ["2018-12-01 00:00:00 UTC", 18]
When I do this for group_by_day, or group_by_week, it gives this:
[Tue, 11 Dec 2018, 0, Wed, 12 Dec 2018, 0, Thu, 13 Dec 2018, 0, Fri, 14 Dec 2018, 0, Sat, 15 Dec 2018, 0, Sun, 16 Dec 2018, 0, Mon, 17 Dec 2018, 0]
What I need is all of "0"'s added up.
Alternative I have working as well:
view:
<% #clients.each do |user| %>
<h3 class="center"><%= user.sales.where(order_status: [2] ).group_by_month(:created_at).count.sum() %></h3>
<% end %>
Output: [Sat, 01 Dec 2018, 18]
I have done more attempt but I don't think listing all of them will help much as it will end up being way too much information.
How can I get back the information:
Orders today
Orders this week
Orders this month
Couldn't find a simple way to remove the date info - the way i "solved" it was by creating a loop with a break so only one result appears.
<% #orders_day.each do |day, orders| %>
<%= orders.count %>
<% break if orders.first %>
<% end %>
It's in DESC order in the controller.
Although, this works for months as well, I couldn't get it to work correctly with weeks. Maybe my internal computers clock was off but the daily would show 3, which is correct, but the weekly would show 1, which makes no sense. Code is code so it's right somehow.
This isn't going to be an accepted response so if someone does have an answer for this questions, feel free to let me know because I will definitely want to hear how you can solve this question.
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 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
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")
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.