0

How can I find the average of 3 date in Ruby on rails or Ruby ? like bellow.

(Date1 + Date2 + Date3)/3

Nagendra
  • 13
  • 2
  • To be clear, are you using **DATES** or **TIMES**? These are different objects, and you probably expect a different result depending on the answer to this question. – Tom Lord Mar 16 '22 at 18:13
  • 3
    The "average" of three *times* is fairly well-defined. But to get the "average" of three dates, you may need to "round up/down"; and the requirements for this are not clear in your question. – Tom Lord Mar 16 '22 at 18:14
  • 2
    It would be helpful if you could [edit the question](https://stackoverflow.com/posts/71502116/edit) to give an example using real values, and potentially highlight how you wish to handle significant edge cases (if there are any!). – Tom Lord Mar 16 '22 at 18:14
  • you can refer this answer: https://stackoverflow.com/a/2288224/17983466 – Sachin Singh Mar 17 '22 at 10:10

3 Answers3

4

If you convert the dates to integers with .to_i you can do the average exactly as you suggested. Then use .at to get back to a datetime.

d1 = Time.now
=> 2022-03-16 11:07:12 -0700

d2 = Time.now - 10000
=> 2022-03-16 08:20:32 -0700

d3 = Time.now - 30000
=> 2022-03-16 02:47:12 -0700

Time.at((d1.to_i + d2.to_i + d3.to_i)/3)
=> 2022-03-16 07:24:58 -0700
mr rogers
  • 3,200
  • 1
  • 19
  • 31
  • 2
    You can also use `to_r` for sub-second precision. – Stefan Mar 16 '22 at 18:15
  • This is working for Date.tody and Date.tommorrow and Date.yesterday and Date.today-1000 ..... but its not works when i get the database convert to integer and doing caluclation and again converting to time its giving the date as 01/01/1970 05:30:00 – Nagendra Mar 18 '22 at 07:06
  • 1
    @Nagendra _"This is working for Date.today and ..."_ – if this code works fine for Ruby's built-in classes, you might consider accepting it as it seems to answer your question (which didn't specify any specific database format). The conversion of your database fields to / from `Date` or `Time` instances seems to be a problem on its own. You could maybe ask that in a separate follow-up question. – Stefan Mar 18 '22 at 09:32
0
first_date = Date.today.strftime("%Y%m%d").to_i
second_date = Date.tomorrow.strftime("%Y%m%d").to_i
third_date = Date.yesterday.strftime("%Y%m%d").to_i

average_date = (first_date + second_date + third_date) / 3

Date.parse(average_date.to_s) OR Time.at(average_date)
  • This is working for Date.tody and Date.tommorrow and Date.yesterday and Date.today-1000 ..... but its not works when i get the database convert to integer and doing caluclation and again converting to time its giving the date as 01/01/1970 05:30:00 – Nagendra Mar 18 '22 at 07:04
  • Date and time calculation doesn't work like that. If you have January 1st and February 1st as integers, i.e. `2022_01_01` and `2022_02_01` (underscores added for clarity), their numeric average is `2022_01_51`. Apparently, that's not a valid date anymore. You could use the Julian day number instead, see [`Date#jd`](https://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html#method-i-jd). – Stefan Mar 18 '22 at 08:31
-4

If you are working with Date objects (Not Time nor DateTime) you can easily calculate the middle date between 3 (or even more dates if you add them to the array).

Imagine you have your dates in an array:

dates = [Date.today, 1.day.ago, 10.days.ago]
difference = dates.max - dates.min
average_date = dates.min + difference/2

Basically, we're getting the days difference between the higher date value and the minimum date value, then adding half that difference to the smallest date, and there you have the average date between the 3, the date in the middle won't change anything so it's not being used with this approach.

Gonzalo Robaina
  • 367
  • 3
  • 15
  • In the OP's **(Date1 + Date2 + Date3)/3** pseudocode, every value matters. – Stefan Mar 16 '22 at 18:21
  • @Stefan I meant it doesn't matter if you get the difference between the min and max dates, I didn't mention that pseudocode. – Gonzalo Robaina Mar 16 '22 at 18:36
  • 2
    Indeed you didn't mention it. But the OP basically asks how to calculate `(1 + 2 + 9)/3` and you suggest doing `(1 + 9)/2` instead. That just doesn't seem right. – Stefan Mar 16 '22 at 18:43
  • From what I can see, the OP asks how to find the average of 3 dates, and I answered exactly that. If you don't like my answer I'm really sorry. – Gonzalo Robaina Mar 16 '22 at 18:49