Given a timestamp, how to determine how many weeks ago? - ruby-on-rails

I'm looking to return the number of weeks since a user was created in my app..
My model is User.rb (id, created_at)
Given user.created_at:
2.4.0 :008 > user.created_at
=> Mon, 14 Aug 2017 15:51:23 UTC +00:00
How can I do something like: user.created_at.weeks_ago where it returns some integer which represents the number of weeks since the user was created?

If you just want to build a human-friendly string, I believe time_ago_in_words is what you are after.
https://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
Otherwise, I would resort to something like the following:
(Time.new - user.created_at) / 1.week
If you want a more elegant solution, Subtract dates in Ruby and get the difference in minutes mentions the Time Difference gem for Ruby.
e.g.
TimeDifference.between(user.created_at.to_time, Time.now).in_weeks

I would use:
Time.current
=> Wed, 20 Sep 2017 03:56:15 UTC +00:00
Then
((Time.current - person.created_at)/604800).to_i
the subtraction gives you the number of seconds then divide it by 604800 which is the number of seconds in a week.

Related

Get time ago from an date that is saved as a string. ruby rails

Im still kinda new.
I saved an date from json data, i saved it in my database as an string. Here by my quistion:
Does anybody have a good idea of how i can calculate how many minutes,hours or days AGO the date is?
"2019-12-09T11:06:37.000Z"
This is what normal dates look like in my database: 2019-12-10 18:28:38.249866
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}

Calculate how many days since creation

Let's say a record has a created_at timestamp of Fri, 16 Feb 2018 18:40:53 UTC +00:00 and I want to find out the amount of days that have passed from the current time. How could I achieve this?
Here is what I've tried:
(Fri, 16 Feb 2018 18:40:53 UTC +00:00 - Time.now).to_i
But that does not work.
Almost, but Ruby doesn't understand your way of writing a datetime.
require 'date'
(DateTime.now - DateTime.new(2018, 2, 16, 18, 40, 53, 0)).to_i
In rails
If you get the timestamp for the model User from the database (not a timestamp string to parse):
seconds_from_creation = Date.today.to_time - user.created_at.to_time
Or directly convert to days (to_i rounds to integer, check if is suitable for you or customise):
((Date.today.to_time - user.created_at.to_time)/(24*60*60)).to_i
In a view you could use the following, which returns a string :
time_ago_in_words user.created_at
# => 10 days (in my case)
You can use
require 'time'
created_at = 'Fri, 16 Feb 2018 18:40:53 UTC +00:00'
(Time.now - Time.parse(created_at)).to_i / 86400

Get Record Created 30 Minutes Ago - Rails

I need to get records created 30 minutes ago from Time.now. I'm using a cron and I want to fire the cron once ever minute.
I essentially need this except it should ignore the seconds so that if Time.now == Wed, 31 Jan 2018 18:00:31 +0000 then a record with created_at = "Wed, 31 Jan 2018 17:30:23 +0000 should match.
Here is the query I have so far which doesn't work because the time is improperly evaluated.
Cart.joins(:cart_addresses).group('carts.id').where(created_at: Time.now - 30.minutes).each do |i|
puts i.id
end
Please try below query:
# if current time is 3:00:03
# it will return carts created at 2:30:00 -> 2:30:59
from_mins_ago = 30.minutes.ago.change(sec: 0)
to_mins_ago = from_mins_ago.change(sec: 59)
Cart.joins(:cart_addresses).group('carts.id').where(created_at: from_mins_ago..to_min_ago).pluck('carts.id')
You could use a range.
Cart.joins(:cart_addresses).group('carts.id').where(created_at: 30.minutes.ago..DateTime.now)
There's a lot of solutions.
More: Rails ActiveRecord date between

How to get time x days ago from a specific date in Rails?

I am looking for a rails solution to calculate the time ago from a particulat time. For example , 2 days ago 15th May 2016 22:00 UTC should return 13th May 2016 22::00 UTC .
My requirement is something like this
2.days.ago.from(yesterday)
Which will be a more specific version of
2.days.from_now
Try this:
> DateTime.now-2.days
=> Wed, 18 May 2016 21:40:31 -0700
how about this:
# 2 days before a specific date
specific_date.days_ago(2)
Example:
specific_date = DateTime.now
two_days_ago_from_specific_date = specific_date.days_ago(2)
My personal favorite syntax for this with rails would be
x.days.ago
For example, if you wanted 10 days ago you would call
10.days.ago
=> Sat, 12 Feb 2022 01:36:58 UTC +00:00
Easy to read and defaults to UTC.
example:
Time.now.ago(10.year)
Time.now.ago(1.minutes)
Time.now.ago(10.day)

Ruby/Rails: converting a Date to a UNIX timestamp

How would I get a UNIX timestamp (number of seconds since 1970 GMT) from a Date object in a Rails app?
I know Time#to_i returns a timestamp, but doing Date#to_time and then getting the timestamp results in something that's off by about a month (not sure why...).
Any help is appreciated, thanks!
Edit: OK, I think I figured it out- I was processing a date several times in a loop, and each time the date was moved a little because of a time zone mismatch, ultimately leading to my timestamp being a month off. Still, I'd be interested in knowing if there's any way to do this without relying on Date#to_time.
The code date.to_time.to_i should work fine. The Rails console session below shows an example:
>> Date.new(2009,11,26).to_time
=> Thu Nov 26 00:00:00 -0800 2009
>> Date.new(2009,11,26).to_time.to_i
=> 1259222400
>> Time.at(1259222400)
=> Thu Nov 26 00:00:00 -0800 2009
Note that the intermediate DateTime object is in local time, so the timestamp might be several hours off from what you expect. If you want to work in UTC time, you can use DateTime's method "utc".
I get the following when I try it:
>> Date.today.to_time.to_i
=> 1259244000
>> Time.now.to_i
=> 1259275709
The difference between these two numbers is due to the fact that Date does not store the hours, minutes or seconds of the current time. Converting a Date to a Time will result in that day, midnight.
Solution for Ruby 1.8 when you have an arbitrary DateTime object:
1.8.7-p374 :001 > require 'date'
=> true
1.8.7-p374 :002 > DateTime.new(2012, 1, 15).strftime('%s')
=> "1326585600"
The suggested options of using to_utc or utc to fix the local time offset does not work. For me I found using Time.utc() worked correctly and the code involves less steps:
> Time.utc(2016, 12, 25).to_i
=> 1482624000 # correct
vs
> Date.new(2016, 12, 25).to_time.utc.to_i
=> 1482584400 # incorrect
Here is what happens when you call utc after using Date....
> Date.new(2016, 12, 25).to_time
=> 2016-12-25 00:00:00 +1100 # This will use your system's time offset
> Date.new(2016, 12, 25).to_time.utc
=> 2016-12-24 13:00:00 UTC
...so clearly calling to_i is going to give the wrong timestamp.
DateTime.new(2012, 1, 15).to_time.to_i

Resources