Determine minutes until midnight

前端 未结 4 715
不思量自难忘°
不思量自难忘° 2020-12-19 01:58

How would you go about determining how many minutes until midnight of the current day using javascript?

4条回答
  •  抹茶落季
    2020-12-19 02:40

    Perhaps:

    function minsToMidnight() {
      var now = new Date();
      var then = new Date(now);
      then.setHours(24, 0, 0, 0);
      return (then - now) / 6e4;
    }
    
    console.log(minsToMidnight());

    or

    function minsToMidnight() {
      var msd = 8.64e7;
      var now = new Date();
      return (msd - (now - now.getTimezoneOffset() * 6e4) % msd) / 6e4;
    }
    
    console.log(minsToMidnight())

    and there is:

    function minsToMidnight(){
      var d = new Date();
      return (-d + d.setHours(24,0,0,0))/6e4;
    }
    
    console.log(minsToMidnight());

提交回复
热议问题