5

How can i make the function round to nearest 15 minute, not rounding down? if its 10:46 it would round to 11:00

jsfiddle: https://jsfiddle.net/31L9d08s/

code:

 var now = new Date();
    var Minutes = now.getMinutes();
    var quarterHours = Math.round(Minutes/15);
    if (quarterHours == 4)
    {
        now.setHours(now.getHours()+1);
    }
    var rounded = (quarterHours*15)%60;
    now.setMinutes(rounded);
    now.setSeconds(0);
    now.setMilliseconds(0);


    console.log(now.getTime());


    var currentdate = new Date(now.getTime()); 
    var datetime = "Last Sync: " + currentdate.getDate() + "/"
                    + (currentdate.getMonth()+1)  + "/" 
                    + currentdate.getFullYear() + " @ "  
                    + currentdate.getHours() + ":"  
                    + currentdate.getMinutes() + ":" 
                    + currentdate.getSeconds();


    console.log(datetime);
maria
  • 207
  • 5
  • 22
  • 56

2 Answers2

9

How about this:

new Date(Math.ceil(new Date().getTime()/900000)*900000);

Explanation: new Date().getTime() returns the current time in the form of a unix timestamp (i.e. the number of milliseconds since 1970-01-01 00:00 UTC), which we round up to the nearest multiple of 900000 (i.e. the number of milliseconds in a quarter-hour) with the help of Math.ceil.

Edit: If you want to apply this to intervals other than 15 minutes, you can do it like that (e.g. for 30 minutes):

var interval = 30 * 60 * 1000; // 30 minutes in milliseconds
new Date(Math.ceil(new Date().getTime()/interval)*interval);
redneb
  • 21,794
  • 6
  • 42
  • 54
  • you probably have to compensate for timezoneOffset. – Thomas Sep 04 '16 at 12:57
  • @Thomas: Why would you need to do that? Are there any timezones that work in something that's not a multiple of 15 minutes? But it's a good point if you're rounding to something that doesn't fit evenly into, say, a half-hour. – T.J. Crowder Sep 04 '16 at 12:58
  • I think @Thomas is actually right. Theoretically, if there is a timezone whose offset from UTC is not a multiple of 15 minutes, the above would not work correctly. – redneb Sep 04 '16 at 13:01
  • could you edit it so i could do something like: $int = 2; then it would be the next 30 min. $int = 3; next 45 min, etc? – maria Sep 04 '16 at 13:08
  • @TJCrowder, yea and nay. In this particular case you don't have to, I thought of a more generic implementation, where "15 Minutes" is just an argument. – Thomas Sep 04 '16 at 13:09
  • @maria You can replace (both occurrences of) 900000 with anything you like, e.g. `(900000 * n)` where `n` is some variable. – redneb Sep 04 '16 at 13:14
  • i tried that @redneb but that would give me incorrect time. – maria Sep 04 '16 at 13:15
  • @maria are you sure you tried it correctly? see my edit – redneb Sep 04 '16 at 13:18
  • would the $interval be if i multiply with two, i would get the next 15 minutes after the first 15 min? if not, how may i? – maria Sep 04 '16 at 14:46
  • @maria I am not sure I understand your question, but if `interval` is 5 min then you get times like :00, :05, :10, :15, etc, or if it was 20 min you get times like :00, :20, :40, etc. – redneb Sep 04 '16 at 14:58
  • can we chat @redneb ? – maria Sep 04 '16 at 16:27
0

Pass any cycle you want in milliseconds to get next cycle example 2,4,8 hours

function calculateNextCycle(interval) {
    const timeStampCurrentOrOldDate = Date.now();
    const timeStampStartOfDay = new Date().setHours(0, 0, 0, 0);
    const timeDiff = timeStampCurrentOrOldDate - timeStampStartOfDay;
    const mod = Math.ceil(timeDiff / interval);
    return new Date(timeStampStartOfDay + (mod * interval));
}

console.log(calculateNextCycle(4 * 60 * 60 * 1000)); // 4 hours in milliseconds