最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - make function round up to nearest quarter hour - Stack Overflow

programmeradmin0浏览0评论

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: /

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);

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/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);
Share Improve this question asked Sep 4, 2016 at 12:49 mariamaria 1595 gold badges24 silver badges62 bronze badges 5
  • 1 This is so much better than the previous time you asked this question. – T.J. Crowder Commented Sep 4, 2016 at 12:52
  • That said, I know you were pointed to this question earlier. Surely your question just involves doing that and adding 15 minutes? – T.J. Crowder Commented Sep 4, 2016 at 12:53
  • it involves that, but not rounding it down @T.J.Crowder – maria Commented Sep 4, 2016 at 12:57
  • 1 Hence the "and adding 15 minutes" – T.J. Crowder Commented Sep 4, 2016 at 12:58
  • can we chat @T.J.Crowder ? – maria Commented Sep 4, 2016 at 16:28
Add a ment  | 

2 Answers 2

Reset to default 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);

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

发布评论

评论列表(0)

  1. 暂无评论