Given a JavaScript date and the task is to round it to 5 minutes with the help of JavaScript. There are two approaches that are discussed below:
Approach 1: In this approach, both options are available to either round down or round up the date object. This example uses basic Math.floor() function and Math.ceil() function to perform the operation.
- Example: This example implements the above approach.
<!DOCTYPE html><html>ÂÂ<head>   Â<title>       ÂRound off a Date Object to 5       Âminutes in JavaScript.   Â</title></head>ÂÂ<bodystyle="text-align:center;">    Â   Â<h1style="color:green;">       Âneveropen   Â</h1>    Â   Â<pid="gfg"style="font-size: 20px;                   Âfont-weight: bold">   Â</p>    Â   Â<buttononclick="GFG_Fun1();">       ÂRound Down   Â</button>    Â   Â<buttononclick="GFG_Fun2();">       ÂRound Up   Â</button>    Â   Â<pid="neveropen"style="font-size: 26px;           Âfont-weight: bold;color: green;">   Â</p>    Â   Â<script>       Âvar up = document.getElementById('gfg');       Âvar down = document.getElementById('neveropen');       Âvar date = new Date();       Âup.innerHTML = "Click on the button to "               Â+ "round the date as specified."               Â+ "<br><br>Date - " + date;       Âfunction GFG_Fun1() {            Â           Â// ms in 5 minutes.           Âvar coff = 1000 * 60 * 5;            Â           Âdown.innerHTML = new Date(                   ÂMath.floor(date / coff) * coff);       Â}       Âfunction GFG_Fun2() {            Â           Â// ms in 5 minutes.           Âvar coff = 1000 * 60 * 5;           Âdown.innerHTML = new Date(                   ÂMath.ceil(date / coff) * coff);       Â}   Â</script></body>ÂÂ</html> -
Output:
Approach 2: This example uses basic Math.round() function to perform the operation. Calculate the milliseconds in 5 minutes, divide the date object by milliseconds and get the round value then again multiply the milliseconds.
- Example: This example implements the above approach.
<!DOCTYPE html><html>ÂÂ<head>   Â<title>       ÂRound off a Date Object to 5       Âminutes in JavaScript.   Â</title></head>ÂÂ<bodystyle="text-align:center;">    Â   Â<h1style="color:green;">       Âneveropen   Â</h1>    Â   Â<pid="GFG_UP"style=       Â"font-size: 20px;font-weight: bold">   Â</p>    Â   Â<buttononclick="GFG_Fun();">       Âclick here   Â</button>    Â   Â<pid="GFG_DOWN"style = "font-size: 26px;           Âfont-weight: bold;color: green;">   Â</p>    Â   Â<script>       Âvar up = document.getElementById('GFG_UP');       Âvar down = document.getElementById('GFG_DOWN');       Âvar date = new Date();       Âup.innerHTML = "Click on the button to "               Â+ "round the date as specified."               Â+ "<br><br>Date - " + date;        Â       Âfunction GFG_Fun() {            Â           Â// ms in 5 minutes.           Âvar coff = 1000 * 60 * 5;           Âdown.innerHTML = new Date(Math.round(                   Âdate.getTime() / coff) * coff);       Â}   Â</script>Â</body>ÂÂ</html>                           -
Output:

