I am using DateTime.now and Time.now methods. And I store it in some variables. Now I want this time in minutes. Meaning, instead of hours and minutes I want to get time in minutes only.
2.2.2 :014 > datetime = DateTime.now
=> Fri, 11 Sep 2015 12:13:00 +0530
2.2.2 :015 > time = Time.now
=> 2015-09-11 12:13:06 +0530
2.2.2 :016 >
Now i want to calculate this time entirely in minutes. is there any method like to_minutes like below?
datetime_in_min = datetime.to_minutes
time_in_min = time.to_minutes
This must have already been done by now but just for anyone who's still looking for an answer, try doing something like below
lets assume the we wish to fetch the minutes and the hours from something like
s = Sat, 27 May 2017 02:30:00 UTC +00:00 (date time)
then,
hours = s.strftime("%H")
minutes = s.strftime("%M")
total_minutes((hours.to_i * 60) + minutes)
hence you'll get something like 150
Hope this helps.
You can use the following method from Time:
Time.now.to_i
to get number of seconds since the Epoch (January 1, 1970 00:00 UTC).
The strftime method can get you any piece of the time you want. The time in minutes (I'm assuming relative to the beginning of the day) you need to do some math:
hours = datetime.strftime('%k').to_i
hours_in_minutes = hours * 60
minutes = datetime.strftime('%M').to_i
minutes_since_start_of_day = hours_in_minutes + minutes
Same thing works for time.
Related
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}
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
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.
I'd like to be able to say user1 is 4 hours ahead of user2, calculated based on the time zones the users specify in their account.
Using the following code:
time1 = Time.zone.now.in_time_zone(user1.time_zone)
time2 = Time.zone.now.in_time_zone(user2.time_zone)
distance_of_time_in_words time1,time2
...gives a difference of less than a minute - similarly subtracting the two times gives 0. Rails obviously still sees these two times as the same.
Any idea how I can calculate this difference between two time zones?
If you take your time1 instance and call utc_offset on it, you will get the amount of time offset from UTC in seconds. Combine this with the utc_offset of time2, throw in some subtraction, and you should get the time difference in seconds. From there you can do the conversation to whatever unit of time you like.
irb(main):020:0> time1 = Time.zone.now.in_time_zone("EST")
=> Sun, 09 Jun 2013 07:11:46 EST -05:00
irb(main):021:0> time2 = Time.zone.now.in_time_zone("MST")
=> Sun, 09 Jun 2013 05:11:49 MST -07:00
irb(main):022:0> time_difference_in_seconds = time2.utc_offset - time1.utc_offset
=> -7200
irb(main):025:0> (time_difference_in_seconds/60/60).abs
=> 2
I have a variable that has a UTC time tied with it, example:
offer_ends_at => Sun, 25 Nov 2012 07:59:59 UTC +00:00,
My goal is to create a new variable, and convert the Time to a seconds integer. I tried with some methods like
seconds = offer_ends_at.to_time.to_i
I end up getting a epoc time. I'm after an actual integer that contains the amount of seconds left until the date happens.
I you want " an actual integer that contains the amount of seconds left until the date happens.", then you can substract the timestamp you're getting, to the current timestamp, ie :
seconds = offer_ends_at.to_time.to_i - Time.now.to_i
In fact, you don't need to put to_i, you can just substract the two times (Time#- method):
seconds = offer_ends_at.to_time - Time.now