Round Date to the Nearest Hour or 30/15/5/1 Minutes in JS

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
10 min

banner

# Table of Contents

  1. Round a Date to the Nearest Hour using JavaScript
  2. Round a Date to the nearest 30 Minutes using JavaScript
  3. Round time to the nearest 15 Minutes using JavaScript
  4. Round a Date to the nearest 5 Minutes using JavaScript
  5. Round time to the nearest Minute using JavaScript

# Round a Date to the Nearest Hour using JavaScript

To round a date to the nearest hour:

  1. Use the setMinutes() method to set the minutes of the date to its current minutes + 30.
  2. Use the setMinutes() method to set the minutes, seconds and milliseconds to 0.
  3. If adding 30 minutes to the date rolls over to the next hour, the hour is rounded up, otherwise down.
index.js
function roundToNearestHour(date) { date.setMinutes(date.getMinutes() + 30); date.setMinutes(0, 0, 0); return date; } // ๐Ÿ‘‡๏ธ Sun Jan 16 2022 14:00:00 (minutes are 30) console.log( roundToNearestHour(new Date(2022, 0, 16, 13, 30, 0)), ); // ๐Ÿ‘‡๏ธ Sun Jan 16 2022 13:00:00 (minutes are 29) console.log( roundToNearestHour(new Date(2022, 0, 16, 13, 29, 0)), );

round date to nearest hour

The code for this article is available on GitHub

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 and time to the nearest hour.

The setMinutes method sets the minutes on the date object.

The method takes the following 3 parameters:

  1. minutesValue - an integer between 0 and 59 that represents the minutes.
  2. secondsValue (optional) - an integer between 0 and 59 that represents the seconds.
  3. msValue (optional) - a number between 0 and 999 that represents the milliseconds.

In our first call to the setMinutes() method, we used the Date.getMinutes() method to get the minutes of the date object and then added 30 to the result.

If adding 30 minutes to the current time increments the hour value by 1, then we should round up to the next nearest hour.

On the other hand, if adding 30 minutes to the time does not increase the hour, then the time is at 29 minutes or less and we should round the minutes down to 0.

The Date object in JavaScript automatically handles the scenario where adding 30 minutes to a date and time rolls over to the next hour and possibly to the next day.

index.js
function roundToNearestHour(date) { date.setMinutes(date.getMinutes() + 30); date.setMinutes(0, 0, 0); return date; } // ๐Ÿ‘‡๏ธ Sun Jan 17 2022 00:00:00 (minutes are 30) console.log(roundToNearestHour(new Date(2022, 0, 16, 23, 30, 0)));
In the example above, we round the hour up, and the Date() object automatically rolls over to the next day because rounding the hour up changes the date.

In our second call to the setMinutes() method, we set the minutes, seconds and milliseconds to 0, to make sure the date and time object points to a round hour, e.g. 08:00:00, and not 08:00:30.

# Table of Contents

  1. Round a Date to the nearest 30 Minutes using JavaScript
  2. Round time to the nearest 15 Minutes using JavaScript
  3. Round a Date to the nearest 5 Minutes using JavaScript
  4. Round time to the nearest Minute using JavaScript

# 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, 0))); // ๐Ÿ‘‡๏ธ Mon Jan 24 2022 06:30:00 (minutes are 15) console.log(roundToNearest30(new Date(2022, 0, 24, 6, 15, 0)));

round date to nearest 30 minutes

The code for this article is available on GitHub

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, 0))); // ๐Ÿ‘‡๏ธ Mon Jan 24 2022 06:30:00 (minutes are 15) console.log(roundToNearest30(new Date(2022, 0, 24, 6, 15, 0)));

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 of 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.

# Table of Contents

  1. Round time to the nearest 15 Minutes using JavaScript
  2. Round a Date to the nearest 5 Minutes using JavaScript
  3. Round time to the nearest Minute using JavaScript

# Round time to the nearest 15 Minutes using JavaScript

To round time to the nearest 15 minutes:

  1. Convert 15 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 15 minutes.
index.js
function roundToNearest15(date = new Date()) { const minutes = 15; 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 09:00:00 (minutes are 7) console.log(roundToNearest15(new Date(2022, 0, 24, 9, 7, 0))); // ๐Ÿ‘‡๏ธ Mon Jan 24 2022 09:15:00 (minutes are 8) console.log(roundToNearest15(new Date(2022, 0, 24, 9, 8, 0)));

round time to nearest 15 minutes

The code for this article is available on GitHub

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 15 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 15 minutes.

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

We divide the result by the number of milliseconds in 15 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(4.49)); // ๐Ÿ‘‰๏ธ 4 console.log(Math.round(4.5)); // ๐Ÿ‘‰๏ธ 5

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 15 minutes, use the Math.ceil() function instead.

index.js
function roundToNearest15(date = new Date()) { const minutes = 15; const ms = 1000 * 60 * minutes; return new Date(Math.ceil(date.getTime() / ms) * ms); } // ๐Ÿ‘‡๏ธ Mon Jan 24 2022 09:15:00 (minutes are 7) console.log(roundToNearest15(new Date(2022, 0, 24, 9, 7, 0))); // ๐Ÿ‘‡๏ธ Mon Jan 24 2022 09:15:00 (minutes are 8) console.log(roundToNearest15(new Date(2022, 0, 24, 9, 8, 0)));

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

index.js
console.log(Math.ceil(4.1)); // ๐Ÿ‘‰๏ธ 5 console.log(Math.ceil(4.0001)); // ๐Ÿ‘‰๏ธ 5

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 of calling Math.round() or Math.ceil() with the number of milliseconds there are in 15 minutes and pass the result to the Date() constructor.

The roundToNearest15 function returns a new Date object with the minutes rounded to the nearest 15.

# Table of Contents

  1. Round a Date to the nearest 5 Minutes using JavaScript
  2. Round time to the nearest Minute using JavaScript

# Round a Date to the nearest 5 Minutes using JavaScript

To round a date to the nearest 5 minutes:

  1. Convert 5 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 5 minutes.
index.js
function roundToNearest5(date = new Date()) { const minutes = 5; const ms = 1000 * 60 * minutes; // ๐Ÿ‘‡๏ธ replace Math.round with Math.ceil to always round UP return new Date(Math.round(date.getTime() / ms) * ms); } // ๐Ÿ‘‡๏ธ Sun Jan 16 2022 08:15:00 (minutes are 13) console.log(roundToNearest5(new Date(2022, 0, 16, 8, 13, 0))); // ๐Ÿ‘‡๏ธ Sun Jan 16 2022 08:10:00 (minutes are 12) console.log(roundToNearest5(new Date(2022, 0, 16, 8, 12, 0)));

round date to nearest 5 minutes

The code for this article is available on GitHub

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 5 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 5 minutes.

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

We divide the result by the number of milliseconds in 5 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(0.49)); // ๐Ÿ‘‰๏ธ 0 console.log(Math.round(0.5)); // ๐Ÿ‘‰๏ธ 1

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 5 minutes, use the Math.ceil function instead.

index.js
function roundToNearest5(date = new Date()) { const minutes = 5; const ms = 1000 * 60 * minutes; return new Date(Math.ceil(date.getTime() / ms) * ms); } // ๐Ÿ‘‡๏ธ Sun Jan 16 2022 08:15:00 (minutes are 13) console.log(roundToNearest5(new Date(2022, 0, 16, 8, 13, 0))); // ๐Ÿ‘‡๏ธ Sun Jan 16 2022 08:15:00 (minutes are 12) console.log(roundToNearest5(new Date(2022, 0, 16, 8, 12, 0)));

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

index.js
console.log(Math.ceil(0.1)); // ๐Ÿ‘‰๏ธ 1 console.log(Math.ceil(5.0001)); // ๐Ÿ‘‰๏ธ 6

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 by the number of milliseconds there are in 5 minutes and pass the result to the Date() constructor.

The roundToNearest5 function returns a new Date object with the minutes rounded to the nearest 5.

# Round time to the nearest Minute using JavaScript

To round time to the nearest minute:

  1. Convert 1 minute 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 1 minute.
index.js
function roundToNearestMinute(date = new Date()) { const minutes = 1; const ms = 1000 * 60 * minutes; // ๐Ÿ‘‡๏ธ replace Math.round with Math.ceil to always round UP return new Date(Math.round(date.getTime() / ms) * ms); } // ๐Ÿ‘‡๏ธ Thu Jan 13 2022 08:30:00 (seconds are 29) console.log(roundToNearestMinute(new Date(2022, 0, 13, 8, 30, 29))); // ๐Ÿ‘‡๏ธ Thu Jan 13 2022 08:31:00 (minutes are 30) console.log(roundToNearestMinute(new Date(2022, 0, 13, 8, 30, 30)));
The code for this article is available on GitHub

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 minute.

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 1 minute.

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

We divide the result by the number of milliseconds in 1 minute 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(2.49)); // ๐Ÿ‘‰๏ธ 2 console.log(Math.round(2.5)); // ๐Ÿ‘‰๏ธ 3

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 minute, use the Math.ceil() function instead.

index.js
function roundToNearestMinute(date = new Date()) { const minutes = 1; const ms = 1000 * 60 * minutes; return new Date(Math.ceil(date.getTime() / ms) * ms); } // ๐Ÿ‘‡๏ธ Thu Jan 13 2022 08:31:00 (seconds are 29) console.log(roundToNearestMinute(new Date(2022, 0, 13, 8, 30, 29))); // ๐Ÿ‘‡๏ธ Thu Jan 13 2022 08:31:00 (minutes are 30) console.log(roundToNearestMinute(new Date(2022, 0, 13, 8, 30, 30)));
The code for this article is available on GitHub

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

index.js
console.log(Math.ceil(2.1)); // ๐Ÿ‘‰๏ธ 3 console.log(Math.ceil(2.0001)); // ๐Ÿ‘‰๏ธ 3

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 of calling Math.round() or Math.ceil() with the number of milliseconds there are in a minute and pass the result to the Date() constructor.

The roundToNearestMinute function returns a new Date object rounding the seconds to the nearest whole minute.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev