Last updated: Mar 5, 2024
Reading timeยท10 min
To round a date to the nearest hour:
setMinutes()
method to set the minutes of the date to its current
minutes + 30
.setMinutes()
method to set the minutes, seconds and milliseconds to
0
.30
minutes to the date rolls over to the next hour, the hour is
rounded up, otherwise down.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)), );
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:
minutesValue
- an integer between 0
and 59
that represents the minutes.secondsValue
(optional) - an integer between 0
and 59
that represents
the seconds.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.
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.
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)));
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
.
To round a date to the nearest 30 minutes:
Math.round()
function.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)));
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.
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.
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.
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.
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.
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
.
To round time to the nearest 15 minutes:
Math.round()
function.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)));
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.
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.
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.
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.
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.
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
.
To round a date to the nearest 5 minutes:
Math.round()
function.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)));
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.
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.
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.
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.
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
.
To round time to the nearest minute:
Math.round()
function.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)));
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.
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.
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.
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.
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 Math.ceil()
function returns the smallest integer that is greater than or
equal to the provided number.
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.
You can learn more about the related topics by checking out the following tutorials: