If current time is between two times using moment

前端 未结 2 1749
不知归路
不知归路 2021-01-16 06:00

I\'m trying to find if the current local time is between two other times using momentjs.

I have the following code:

var currentTime = moment().format         


        
2条回答
  •  灰色年华
    2021-01-16 06:37

    Don't format the dates you get back from moment if you're planning on comparing them.

    format() returns a string, not a date/time-comparable object.

    var currentTime = moment();
    
    var extra = moment().format('YYYY-MM-DD') + ' ';
    var start_time = moment(extra + '16:00');
    var end_time = moment(extra + '16:30');
    
    if (moment(currentTime).isBetween(start_time, end_time))
      console.log('TRUE');
    else
      console.log('FALSE');

提交回复
热议问题