Round a Date to the nearest 30 Minutes using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 16, 2022

banner

Photo from Unsplash

Round a Date to the nearest 30 Minutes using JavaScript #

To round a date to the nearest 30 minutes:

  1. Convert 30 minutes to milliseconds.
  2. Divide the date's value in milliseconds by the result from the conversion.
  3. Pass the result to the Math.round() function.
  4. Multiply by the number of milliseconds in 30 minutes.
index.js
function roundToNearest30(date = new Date()) { const minutes = 30; const ms = 1000 * 60 * minutes; // 👇️ replace Math.round with Math.ceil to always round UP return new Date(Math.round(date.getTime() / ms) * ms); } // 👇️ Mon Jan 24 2022 06:00:00 (minutes are 14) console.log(roundToNearest30(new Date(2022, 0, 24, 6, 14, 00))); // 👇️ Mon Jan 24 2022 06:30:00 (minutes are 15) console.log(roundToNearest30(new Date(2022, 0, 24, 6, 15, 00)));

We used the Date() constructor when logging the examples to the console. The parameters we passed are: year, month (January = 0, February = 1, etc), day of month, hours, minutes, seconds.

We created a reusable function that rounds a date to the nearest 30 minutes.

The function takes a Date object as a parameter or uses the current date and time if no parameter is provided.

If you always want to round up, replace the Math.round() function with Math.ceil().

The ms variable stores the number of milliseconds there are in 30 minutes.

The getTime method returns the number of milliseconds since the Unix Epoch.

We divide the result by the number of milliseconds in 30 minutes and round to the nearest integer using the Math.round function.

Here are some examples of how Math.round works.

index.js
console.log(Math.round(6.49)); // 👉️ 6 console.log(Math.round(6.5)); // 👉️ 7

The function rounds the number up or down to the nearest integer.

If the number is positive and its fractional part is greater than or equal to 0.5, it gets rounded to the next higher absolute value.

If the number is positive and its fractional portion is less than 0.5, it gets rounded to the lower absolute value.

If you always want to round up to the next 30 minutes, use the Math.ceil function instead.

index.js
function roundToNearest30(date = new Date()) { const minutes = 30; const ms = 1000 * 60 * minutes; return new Date(Math.ceil(date.getTime() / ms) * ms); } // 👇️ Mon Jan 24 2022 06:30:00 (minutes are 14) console.log(roundToNearest30(new Date(2022, 0, 24, 6, 14, 00))); // 👇️ Mon Jan 24 2022 06:30:00 (minutes are 15) console.log(roundToNearest30(new Date(2022, 0, 24, 6, 15, 00)));

The Math.ceil function returns the smallest integer that is greater than or equal to the provided number.

index.js
console.log(Math.ceil(6.1)); // 👉️ 7 console.log(Math.ceil(6.0001)); // 👉️ 7

In short, if there is anything after the decimal, the number will get rounded to the next integer, otherwise the number is returned.

The last step is to multiply the value from calling Math.round or Math.ceil with the number of milliseconds there are in 30 minutes and pass the result to the Date() constructor.

The roundToNearest30 function returns a new Date object with the minutes rounded to the nearest 30.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.