Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require 'time'
def average_time_of_day(array_of_times)
total = array_of_times.inject(0) { |total,current| total + Time.parse(current).to_i } / array_of_times.size).strftime('%I:%M %p').downcase!
# Assumes same day
puts average_time_of_day(["12:00 am", "11:59 pm", "11:53 pm"])
# Also works
puts average_time_of_day(["Nov 2 12:00 am", "Nov 1 11:59 pm", "Nov 1 11:53 pm"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment