How to use Moment.JS to check whether the current time is between 2 times

前端 未结 3 1985
情深已故
情深已故 2020-12-15 15:00

Say the current time is 09:34:00 (hh:mm:ss), and I have two other times in two variables:

var beforeTime = \'08:34:00\',
    afterT         


        
3条回答
  •  粉色の甜心
    2020-12-15 15:38

    • You can pass moment instances to isBetween()
    • leave out the format() calls, what you want is to pass parse formats like int the first moment() of your second attempt.

    That's all:

    var format = 'hh:mm:ss'
    
    // var time = moment() gives you current time. no format required.
    var time = moment('09:34:00',format),
      beforeTime = moment('08:34:00', format),
      afterTime = moment('10:34:00', format);
    
    if (time.isBetween(beforeTime, afterTime)) {
    
      console.log('is between')
    
    } else {
    
      console.log('is not between')
    
    }
    
    // prints 'is between'
    

提交回复
热议问题