Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript date get nearest 5 minutes

let interval = 15 * 60 * 1000;  // 15min in ms
let myDate = new Date(); // date that should be round to nearest 15min

// date with nearest 15min
let newDate = new Date(Math.round(myDate / interval) * interval); 
// note: for NEXT 15min interval, use Math.ceil()

// examples
// myDate('2022-08-11 09:05:13') -> newDate = '2022-08-11 09:00:00'
// myDate('2022-08-11 09:08:11') -> newDate = '2022-08-11 09:15:00'
// myDate('2022-08-11 09:51:16') -> newDate = '2022-08-11 09:45:00'
// myDate('2022-08-11 09:52:48') -> newDate = '2022-08-11 10:00:00'
 
PREVIOUS NEXT
Tagged: #javascript #date #nearest #minutes
ADD COMMENT
Topic
Name
5+6 =