7

Expected Result

Round Off Time : 15 min

Given Time 10:00 => Rounded to: 10:00

Given Time 10:13 => Rounded to: 10:15

Given Time 10:15 => Rounded to: 10:15

Given Time 10:16 => Rounded to: 10:30

Given Time 16:00 => Rounded to: 16:00

Given Time 16:12 => Rounded to: 16:15

Round Off Time varies based on user input

MyCode

var m = (((minutes + 7.5)/roundOffTime | 0) * roundOffTime) % 60;
var h = ((((minutes/105) + .5) | 0) + hours) % 24;

Current Output

Given time: 08:22 => Rounded to: 08:15

Given time: 08:23 => Rounded to: 08:30

Need round off time should be in increment order

siva
  • 593
  • 3
  • 8
  • 19

2 Answers2

7

You could take all minutes and divide by 15 for getting the whole quarter and multiply by 15 for the result. Then take hours and minutes and apply formatting. Return joined values.

function roundMinutes(t) {
    function format(v) { return v < 10 ? '0' + v: v; }

    var m = t.split(':').reduce(function (h, m) { return h * 60 + +m; });
    
    m = Math.ceil(m / 15) * 15;
    return [Math.floor(m / 60), m % 60].map(format).join(':');
}

var data = ['10:00', '10:13', '10:15', '10:16', '16:00', '16:12', '16:55'];

console.log(data.map(roundMinutes));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • *…showed amazing focus and adherence to basic KISS principle in not being distracted by Date manipulations and formatting…* – RobG Sep 07 '17 at 06:12
4

Try this:

var coeff = 1000 * 60 * 5;
var date = new Date();  //or use any other date
console.log(date);
var rounded = new Date(Math.round(date.getTime() / coeff) * coeff);
console.log(rounded);
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108