Subtracting Dates with Ruby

Subtracting dates with Ruby

You want Date instead of Time:

require 'date'

now = Date.today
before = Date.civil(2000, 1, 1)
difference_in_days = (now - before).to_i

(difference_in_days/365.25).to_i

Will give you the difference in years between today and January 1st 2000. It can probably be improved, I just used the average number of days per year (365.25), which will give you the right answer except in extreme edge cases.

You can also do something like this:

require 'date'

years = 0
d = Date.civil(2000, 1, 1)
loop do
d = d.next_year
break if Date.today < d
years += 1
end

But Date#next_year was introduced in Ruby 1.9, so it wouldn't work in 1.8.7.

Of course, the easiest way of determining the number of years between two dates is just subtracting the numbers:

2010 - 2000 # => 10

DateTime subtraction in ruby 2?

When you substract two datetimes, you'll get the difference in days, not hours.

You get a Rational type for the precision (some float numbers cannot be expressed exactly with computers)

To get a number of hours, multiply the result by 24, for minutes multiply by 24*60 etc...

a = DateTime.new(2015, 6, 20, 16)
b = DateTime.new(2015, 6, 21, 16)

(a - b).to_i
# days
# => -1

((a - b)* 24).to_i
# hours
# => -24
# ...

Here's a link to the official doc

Subtracting dates in ruby

It looks like date_time is already a DateTime object, so why convert it to a string and reparse it?

require 'date'

date_time = DateTime.parse('Fri Jul 26 00:40:12 -0700 2013')
date_time.class # => DateTime
(Date.today - date_time).to_i # => 4

If you want to ignore the time component of the DateTime, convert it to a date:

require 'date'

date_time = DateTime.parse('Fri Jul 26 00:40:12 -0700 2013')
date_time.class # => DateTime
(Date.today - date_time.to_date).to_i # => 5

Subtracting dates in ruby or rails

Date and Time are not the same class so you can't just subtract one from the other.

Try using DateTime instead or coercing time into a date using to_date.

due_date = DateTime.parse '2020-07-18 00:00:00 -0700'
today_date = DateTime.current
(due_date - today_date).to_i

Also be careful using Time.now because it's not time zone aware (https://thoughtbot.com/blog/its-about-time-zones)

Subtract dates in Ruby and get the difference in minutes

(time1 - time2) / 60

If the time objects are string, Time.parse(time) them first

Subtracting two dates in ruby on rails

try this, for example

require 'date'
$ now = Date.today
$ before = Date.today + 2.days
$ difference_in_days = (before - now).to_i

for your solution

def total_days
difference_in_days = (self.to_date - self.from_date).to_i
end

Ruby Date Subtraction (e.g. 90 days Ago)

require 'date'
now = Date.today
ninety_days_ago = (now - 90)

Running this thru the IRB console I get:

>>require 'date'
now = Date.today
ninety_days_ago = (now - 90)

require 'date'
=> false
now = Date.today
=> #<Date: 2011-03-02 (4911245/2,0,2299161)>
ninety_days_ago = (now - 90)
=> #<Date: 2010-12-02 (4911065/2,0,2299161)>


If you need the time you could just say now = DateTime.now

Ruby Date Time Subtraction

my_array = ["Some_xyz_process",
"Start", "2018-07-12", "12:59:53,397",
"End", "2018-07-12", "12:59:55,913"]

require 'date'

fmt = '%Y-%m-%d%H:%M:%S,%L'
is = my_array.index('Start')
#=> 1
ie = my_array.index('End')
#=> 4
DateTime.strptime(my_array[ie+1] + my_array[ie+2], fmt).to_time -
DateTime.strptime(my_array[is+1] + my_array[is+2], fmt).to_time
#=> 2.516 (seconds)

See DateTime#strptime and DateTime# (the latter for format directives). As long as the date and time formats are known I always prefer strptime to parse. Here's an example of why:

DateTime.parse 'Theresa May has had a bad week over Brexit'
#=> #<DateTime: 2018-05-01T00:00:00+00:00 ((2458240j,0s,0n),+0s,2299161j)>`.

How can I subtract formatted dates in Ruby, to return a day difference?

(date1.to_date - date2.to_date).to_i should give you the difference in days.



Related Topics



Leave a reply



Submit