JavaScript Round to the nearest n hour round nearest hour excel,nearest round hour,round to nearest

I'm trying to round a Date to the nearest 3, 6, 12, or 24 hours intervals (starting at midnight). Given the following inputs, I'm looking for the related output..

for 24 hours
2013-09-11 00:00:00 -> 2013-09-11 00:00:00 (no change needed)
2013-09-11 01:30:25 -> 2013-09-11 00:00:00 (rounded down)
2013-09-11 12:01:01 -> 2013-09-12 00:00:00 (rounded up)

for 12 hours
2013-09-11 00:00:00 -> 2013-09-11 00:00:00 (no change needed)
2013-09-11 12:00:00 -> 2013-09-11 12:00:00 (no change needed)
2013-09-11 11:50:57 -> 2013-09-11 12:00:00 (rounded up)
2013-09-11 12:01:00 -> 2013-09-11 12:00:00 (rounded down)
2013-09-11 22:15:48 -> 2013-09-12 00:00:00 (rounded up)

for 6 hours
2013-09-11 05:50:57 -> 2013-09-11 06:00:00 (rounded up)
2013-09-11 08:50:57 -> 2013-09-11 06:00:00 (rounded down)
2013-09-11 10:50:57 -> 2013-09-11 12:00:00 (rounded up)

for 3 hours
2013-09-11 01:50:57 -> 2013-09-11 00:00:00 (rounded down)
2013-09-11 02:50:57 -> 2013-09-11 03:00:00 (rounded up)
2013-09-11 09:40:57 -> 2013-09-11 09:00:00 (rounded down)

etc...

I keep finding all of these interesting solutions for rounding to the nearest minute, but I can't seem to fit them to my own needs. I thought I had something going using reference #2 below, but it fails miserably (http://jsfiddle.net/LUwk8/2/). Any ideas?

References:

  1. Round Date to nearest 15 minute interval in Flex
  2. How to round time to the nearest quarter hour in JavaScript?
Answer:1

Get the fractional hours using [get|set][Hours|Minutes|Seconds|Milliseconds](), and round that to the interval:

function roundTo(num, interval) {
    return Math.round(num / interval) * interval;
}

function roundHours(date, interval) {
    var newDate = new Date(date);
    var h = newDate.getHours() + newDate.getMinutes() / 60 + newDate.getSeconds() / 3600 + newDate.getMilliseconds() / 3600000;
    newDate.setMinutes(0);
    newDate.setSeconds(0);
    newDate.setMilliseconds(0);
    newDate.setHours(roundTo(h, interval));

    return newDate;
}
Answer:2

You can use the mod of the time divided by the precision,

if you want to make 1am round down to midnight,

rather than up to 3, 6 or noon.

function roundHours(precision, d){
    precision= precision || 1;
    d= d? new Date(d):new Date();
    if(d.getSeconds()>30) d.setMinutes(d.getMinutes()+1);
    if(d.getMinutes>30) hours+= 1;
    var hours= d.getHours(), diff= hours%precision;
    if(diff>precision/2) hours+= (precision-diff);
    else hours-= diff;
    d.setHours(hours, 0, 0, 0);
    return d.toLocaleString();
}

//testing precision:

var A= [], range=[3,6,12,24], d1= new Date();
for(var x= 0; x<4; x++){
    A.push('\n'+range[x]+ ' hour precision:');
    for(var i= 0; i<24; i++){
        d1.setHours(i);
        A.push(i+':  '+roundHours(range[x], d1));
    }
}
A.join('\n');

//returned value: (String)

3 hour precision:
00:  Wednesday, September 11, 2013 12:00:00 AM
01:  Wednesday, September 11, 2013 12:00:00 AM
02:  Wednesday, September 11, 2013 3:00:00 AM
03:  Wednesday, September 11, 2013 3:00:00 AM
04:  Wednesday, September 11, 2013 3:00:00 AM
05:  Wednesday, September 11, 2013 6:00:00 AM
06:  Wednesday, September 11, 2013 6:00:00 AM
07:  Wednesday, September 11, 2013 6:00:00 AM
08:  Wednesday, September 11, 2013 9:00:00 AM
09:  Wednesday, September 11, 2013 9:00:00 AM
10:  Wednesday, September 11, 2013 9:00:00 AM
11:  Wednesday, September 11, 2013 12:00:00 PM
12:  Wednesday, September 11, 2013 12:00:00 PM
13:  Wednesday, September 11, 2013 12:00:00 PM
14:  Wednesday, September 11, 2013 3:00:00 PM
15:  Wednesday, September 11, 2013 3:00:00 PM
16:  Wednesday, September 11, 2013 3:00:00 PM
17:  Wednesday, September 11, 2013 6:00:00 PM
18:  Wednesday, September 11, 2013 6:00:00 PM
19:  Wednesday, September 11, 2013 6:00:00 PM
20:  Wednesday, September 11, 2013 9:00:00 PM
21:  Wednesday, September 11, 2013 9:00:00 PM
22:  Wednesday, September 11, 2013 9:00:00 PM
23:  Thursday, September 12, 2013 12:00:00 AM

6 hour precision:
00:  Wednesday, September 11, 2013 12:00:00 AM
01:  Wednesday, September 11, 2013 12:00:00 AM
02:  Wednesday, September 11, 2013 12:00:00 AM
03:  Wednesday, September 11, 2013 12:00:00 AM
04:  Wednesday, September 11, 2013 6:00:00 AM
05:  Wednesday, September 11, 2013 6:00:00 AM
06:  Wednesday, September 11, 2013 6:00:00 AM
07:  Wednesday, September 11, 2013 6:00:00 AM
08:  Wednesday, September 11, 2013 6:00:00 AM
09:  Wednesday, September 11, 2013 6:00:00 AM
10:  Wednesday, September 11, 2013 12:00:00 PM
11:  Wednesday, September 11, 2013 12:00:00 PM
12:  Wednesday, September 11, 2013 12:00:00 PM
13:  Wednesday, September 11, 2013 12:00:00 PM
14:  Wednesday, September 11, 2013 12:00:00 PM
15:  Wednesday, September 11, 2013 12:00:00 PM
16:  Wednesday, September 11, 2013 6:00:00 PM
17:  Wednesday, September 11, 2013 6:00:00 PM
18:  Wednesday, September 11, 2013 6:00:00 PM
19:  Wednesday, September 11, 2013 6:00:00 PM
20:  Wednesday, September 11, 2013 6:00:00 PM
21:  Wednesday, September 11, 2013 6:00:00 PM
22:  Thursday, September 12, 2013 12:00:00 AM
23:  Thursday, September 12, 2013 12:00:00 AM

12 hour precision:
00:  Wednesday, September 11, 2013 12:00:00 AM
01:  Wednesday, September 11, 2013 12:00:00 AM
02:  Wednesday, September 11, 2013 12:00:00 AM
03:  Wednesday, September 11, 2013 12:00:00 AM
04:  Wednesday, September 11, 2013 12:00:00 AM
05:  Wednesday, September 11, 2013 12:00:00 AM
06:  Wednesday, September 11, 2013 12:00:00 AM
07:  Wednesday, September 11, 2013 12:00:00 PM
08:  Wednesday, September 11, 2013 12:00:00 PM
09:  Wednesday, September 11, 2013 12:00:00 PM
10:  Wednesday, September 11, 2013 12:00:00 PM
11:  Wednesday, September 11, 2013 12:00:00 PM
12:  Wednesday, September 11, 2013 12:00:00 PM
13:  Wednesday, September 11, 2013 12:00:00 PM
14:  Wednesday, September 11, 2013 12:00:00 PM
15:  Wednesday, September 11, 2013 12:00:00 PM
16:  Wednesday, September 11, 2013 12:00:00 PM
17:  Wednesday, September 11, 2013 12:00:00 PM
18:  Wednesday, September 11, 2013 12:00:00 PM
19:  Thursday, September 12, 2013 12:00:00 AM
20:  Thursday, September 12, 2013 12:00:00 AM
21:  Thursday, September 12, 2013 12:00:00 AM
22:  Thursday, September 12, 2013 12:00:00 AM
23:  Thursday, September 12, 2013 12:00:00 AM

24 hour precision:
00:  Wednesday, September 11, 2013 12:00:00 AM
01:  Wednesday, September 11, 2013 12:00:00 AM
02:  Wednesday, September 11, 2013 12:00:00 AM
03:  Wednesday, September 11, 2013 12:00:00 AM
04:  Wednesday, September 11, 2013 12:00:00 AM
05:  Wednesday, September 11, 2013 12:00:00 AM
06:  Wednesday, September 11, 2013 12:00:00 AM
07:  Wednesday, September 11, 2013 12:00:00 AM
08:  Wednesday, September 11, 2013 12:00:00 AM
09:  Wednesday, September 11, 2013 12:00:00 AM
10:  Wednesday, September 11, 2013 12:00:00 AM
11:  Wednesday, September 11, 2013 12:00:00 AM
12:  Wednesday, September 11, 2013 12:00:00 AM
13:  Thursday, September 12, 2013 12:00:00 AM
14:  Thursday, September 12, 2013 12:00:00 AM
15:  Thursday, September 12, 2013 12:00:00 AM
16:  Thursday, September 12, 2013 12:00:00 AM
17:  Thursday, September 12, 2013 12:00:00 AM
18:  Thursday, September 12, 2013 12:00:00 AM
19:  Thursday, September 12, 2013 12:00:00 AM
20:  Thursday, September 12, 2013 12:00:00 AM
21:  Thursday, September 12, 2013 12:00:00 AM
22:  Thursday, September 12, 2013 12:00:00 AM
23:  Thursday, September 12, 2013 12:00:00 AM
Answer:3

I'm trying to get document first children (which means: body, html, head etc..) This code line works: var x = document.body.children; but this one does not: var x = document.children; how can I ...

I'm trying to get document first children (which means: body, html, head etc..) This code line works: var x = document.body.children; but this one does not: var x = document.children; how can I ...

  1. document of children's rights

The below code is an attempt to create a modular code block, which silently fails in case jQuery (dependency) is not present. (function($, undefined){ if ( $ === undefined ){ // Do some ...

The below code is an attempt to create a modular code block, which silently fails in case jQuery (dependency) is not present. (function($, undefined){ if ( $ === undefined ){ // Do some ...

  1. $handle undefined function/2
  2. handle function undefined
  3. javascript handle undefined function
  4. javascript handle undefined function call
  5. undefined function handle matlab
  6. wp_handle_upload undefined function

I have the following following code: for (i = 0; i < 5; i++) { this.hands[0].cards[i].img.on('mousedown', (function (i) { var j = i; return function (event) { self....

I have the following following code: for (i = 0; i < 5; i++) { this.hands[0].cards[i].img.on('mousedown', (function (i) { var j = i; return function (event) { self....

I am writing in Casper Javacript and I am writing a test for a search bar object that tests each letter of the alphabet, by itself. So it will test 'a' then 'b' and so on. I am using the sendKeys ...

I am writing in Casper Javacript and I am writing a test for a search bar object that tests each letter of the alphabet, by itself. So it will test 'a' then 'b' and so on. I am using the sendKeys ...

  1. reset search field jquery
  2. datatables reset search field