round up/ round down a momentjs moment to nearest minute

后端 未结 10 1424
一整个雨季
一整个雨季 2020-12-05 01:47

How do you round up/ round down a momentjs moment to nearest minute?

I have checked the docs, but there doesn\'t appear to be a method for this.

Note that I

相关标签:
10条回答
  • 2020-12-05 02:11

    I was searching for this same question and found a better solution: Use the third parameter in diff() function:

    moment("2019-05-02 17:10:20").diff("2019-05-02 17:09:30","minutes",true)

    By setting third parameter to true, you get the raw value as response that you can round by yourself using Math.round()

    See JSFiddle: https://jsfiddle.net/2wqs4o0v/3/

    0 讨论(0)
  • 2020-12-05 02:13

    A more precise answer:

    t.add(30, 'seconds').startOf('minute')
    

    Case1: Rounding down if seconds < 30

    t = moment(); //12:00:05
    t.add(30, 'seconds').startOf('minute') //12:00:00
    

    Case2: Rounding up if seconds >= 30

    t = moment(); //12:00:33
    t.add(30, 'seconds').startOf('minute') //12:01:00
    
    0 讨论(0)
  • 2020-12-05 02:18

    The simplest solution so far:

    function floor(time, floorBy = 'minute') {
      return time.startOf(floorBy);
    }
    
    function ceil(time, ceilBy = 'minute') {
      return time.subtract(1, 'millisecond').add(1, ceilBy).startOf(ceilBy);
    }
    
    // The solution is above. The code below is an optional test:
    
    console.log(
      floor(moment('2019-01-01 12:00:00.000')).format('H:mm:ss.SSS') === '12:00:00.000',
       ceil(moment('2019-01-01 12:00:00.000')).format('H:mm:ss.SSS') === '12:00:00.000',
      floor(moment('2019-01-01 12:00:00.001')).format('H:mm:ss.SSS') === '12:00:00.000',
       ceil(moment('2019-01-01 12:00:00.001')).format('H:mm:ss.SSS') === '12:01:00.000',
      floor(moment('2019-01-01 12:15:16.876'), 'hour'  ).format('H:mm:ss.SSS') === '12:00:00.000',
       ceil(moment('2019-01-01 12:15:16.876'), 'hour'  ).format('H:mm:ss.SSS') === '13:00:00.000',
      floor(moment('2019-01-01 12:59:59.999'), 'second').format('H:mm:ss.SSS') === '12:59:59.000',
       ceil(moment('2019-01-01 12:59:59.999'), 'second').format('H:mm:ss.SSS') === '13:00:00.000',
      floor(moment('2019-01-01 12:00:00.025'), 'ms'    ).format('H:mm:ss.SSS') === '12:00:00.025',
       ceil(moment('2019-01-01 12:00:00.025'), 'ms'    ).format('H:mm:ss.SSS') === '12:00:00.025'
    );
    <script src="//cdn.jsdelivr.net/npm/moment@2.24.0/min/moment.min.js"></script>

    0 讨论(0)
  • 2020-12-05 02:22

    Rounding Down

    Easy. As stated by many others, just use Moment.startOf:

    var roundDown = moment('2015-02-17 12:59:59').startOf('hour');
    roundDown.format('HH:mm:SS'); // 12:00:00
    

    Importantly, this also works as expected:

    var roundDown = moment('2015-02-17 12:00:00').startOf('hour');
    roundDown.format('HH:mm:SS'); // 12:00:00
    

    Rounding Up

    Slightly trickier, if we want to round up with a proper ceiling function: for example, when rounding up by hour, we want 12:00:00 to round up to 12:00:00.

    This does not work

    var roundUp = moment('2015-02-17 12:00:00').add(1, 'hour').startOf('hour');
    roundUp.format('HH:mm:SS'); // ERROR: 13:00:00
    

    Solution

    function roundUp(momentObj, roundBy){
      return momentObj.add(1, roundBy).startOf(roundBy);
    }
    
    
    var caseA = moment('2015-02-17 12:00:00');
    roundUp(caseA, 'minute').format('HH:mm:SS'); // 12:00:00
    
    var caseB = moment('2015-02-17 12:00:00.001');
    roundUp(caseB, 'minute').format('HH:mm:SS'); // 12:01:00
    
    var caseC = moment('2015-02-17 12:00:59');
    roundUp(caseC, 'minute').format('HH:mm:SS'); // 12:01:00
    
    0 讨论(0)
提交回复
热议问题