Reason #105 • April 15th, 2026

ActiveSupport: Time#beginning_of_day and friends

Working with time in programming can be quite a drag, but ActiveSupport provides a host of methods to make it all the more pleasant. One such set of methods is the beginning_of_* and end_of_* family:

Ruby
time = Time.new(2026, 4, 7, 15, 42, 10, "+00:00")

time.beginning_of_day
# => 2026-04-07 00:00:00 +0000

time.end_of_day
# => 2026-04-07 23:59:59.999999 +0000

time.beginning_of_month
# => 2026-04-01 00:00:00 +0000

time.end_of_week
# => 2026-04-12 23:59:59.999999 +0000
      
JavaScript
const beginningOfDay = d =>
  new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));

const endOfDay = d =>
  new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), 23, 59, 59, 999));

const beginningOfMonth = d =>
  new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), 1));

const endOfWeek = d => {
   // Monday-based week => Sunday end
  const daysUntilSunday = (7 - d.getUTCDay()) % 7;
  return new Date(Date.UTC(
    d.getUTCFullYear(),
    d.getUTCMonth(),
    d.getUTCDate() + daysUntilSunday,
    23, 59, 59, 999
  ));
};

// month is 0-based
const time = new Date(Date.UTC(2026, 3, 7, 15, 42, 10));
beginningOfDay(time)
// 2026-04-07T00:00:00.000Z

endOfDay(time)
// 2026-04-07T23:59:59.999Z

beginningOfMonth(time)
// 2026-04-01T00:00:00.000Z

endOfWeek(time)
// 2026-04-12T23:59:59.999Z
      

Intuitive and clear-cut!

History

The original Time::Calculations extensions, including beginning_of_day and beginning_of_month, were already present in ActiveSupport 0.10.0, released in 2005. end_of_day followed in Rails 2.0.0 in 2007, and end_of_week arrived in Rails 2.1.0 in 2008.