Ago date/time functions in Ruby/Rails
You can use:
10.minutes.ago
2.days.since
Or in your views you have the helpers:
distance_of_time_in_words(from_time, to_time)
time_ago_in_words(from_time)
Check the API for details and more options.
How to get time x days ago from a specific date in Rails?
Try this:
> DateTime.now-2.days
=> Wed, 18 May 2016 21:40:31 -0700
Get time ago from an date that is saved as a string. ruby rails
The easiest way if you want to avoid doing a bunch of math is by using ActiveSupport::Duration.
irb(main):001:0> time = Time.zone.parse("2019-12-09T11:06:37.000Z")
=> Mon, 09 Dec 2019 11:06:37 UTC +00:00
irb(main):002:0> duration = ActiveSupport::Duration.build(Time.zone.now - time)
=> 1 day, 11 hours, 10 minutes, and 31.248518 seconds
irb(main):003:0> duration.parts
=> {:days=>1, :hours=>11, :minutes=>10, :seconds=>31.248518}
Display datetime like 1 hour ago, 5 hours ago, 3 days ago,
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words
Ruby Date 1 month ago to String
Try this:
require 'date'
d = Date.today.prev_month # or Date.today.next_month, depending what you want
=> #<Date: 2014-12-27 ((2457019j,0s,0n),+0s,2299161j)>
d.strftime("%FT%T")
=> "2014-12-27T00:00:00"
Time ago in created_at?
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_ago_in_words
so it should be
<%= time_ago_in_words(comment.created_at) %>
Rails: How I can get yesterday's date?
Rails
For a date object you could use:
Date.yesterday
Or a time object:
1.day.ago
Ruby
Or outside of rails:
require 'date'
Date.today.prev_day
Related Topics
How to Open Files Relative to Home Directory
Rails Console Running Incredibly Slowly When Editing Text
Disable Rvm or Use Ruby Which Was Installed Without Rvm
Is Ruby a Scripting Language or an Interpreted Language
Possible to Alias a Belongs_To Association in Rails
Error Message "Xcode Alone Is Not Sufficient on Sierra"
"Ago" Date/Time Functions in Ruby/Rails
How to Create a Rest API for a Ruby Application
Rails Collection_Select VS. Select
Passing Param Values to Redirect_To as Querystring in Rails
Cleanest Way to Create a Hash from an Array