Javascript 如何使用 Moment.JS 检查当前时间是否在 2 倍之间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36197031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:44:56  来源:igfitidea点击:

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

javascriptmomentjs

提问by ????

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

假设当前时间是09:34:00( hh:mm:ss),我在两个变量中还有另外两个时间:

var beforeTime = '08:34:00',
    afterTime = '10:34:00';

How do I use Moment.JS to check whether the current time is between beforeTimeand afterTime?

如何使用 Moment.JS 检查当前时间是否在beforeTime和之间afterTime

I've seen isBetween(), and I've tried to use it like:

我见过isBetween(),我试过像这样使用它:

moment().format('hh:mm:ss').isBetween('08:27:00', '10:27:00')

but that doesn't work because as soon as I format the first (current time) moment into a string, it's no longer a moment object. I've also tried using:

但这不起作用,因为一旦我将第一个(当前时间)时刻格式化为字符串,它就不再是一个时刻对象。我也试过使用:

moment('10:34:00', 'hh:mm:ss').isAfter(moment().format('hh:mm:ss')) && moment('08:34:00', 'hh:mm:ss').isBefore(moment().format('hh:mm:ss'))

but I get false, because again when I format the current time, it's no longer a moment.

但我明白了false,因为当我再次格式化当前时间时,它不再是片刻。

How do I get this to work?

我如何让这个工作?

回答by Linus Borg

  • 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.
  • 您可以将时刻实例传递给 isBetween()
  • 省略format()调用,您想要的是在第二次尝试的第一时刻()传递解析格式,如 int 。

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'

回答by Woppi

I had to use isBetweenand isSamein my case to cover the beforeand aftertime I used in the isBetween condition.

我不得不使用isBetweenisSame在我的情况下,以覆盖beforeafter我在isBetween条件下使用的时间。

function getTimeCategory(time) {
  let timeCategory = '';
  const timeFormat = 'HH:mm:ss';

  if (
    time.isBetween(moment('00:00:00', timeFormat), moment('04:59:59', timeFormat)) ||
    time.isSame(moment('00:00:00', timeFormat)) ||
    time.isSame(moment('04:59:59', timeFormat))
  ) {
    timeCategory = 'DAWN';
  } else if (
    time.isBetween(moment('05:00:00', timeFormat), moment('11:59:59', timeFormat)) ||
    time.isSame(moment('05:00:00', timeFormat)) ||
    time.isSame(moment('11:59:59', timeFormat))
  ) {
    timeCategory = 'MORNING';
  } else if (
    time.isBetween(moment('12:00:00', timeFormat), moment('16:59:59', timeFormat)) ||
    time.isSame(moment('12:00:00', timeFormat)) ||
    time.isSame(moment('16:59:59', timeFormat))
  ) {
    timeCategory = 'NOON';
  } else if (
    time.isBetween(moment('17:00:00', timeFormat), moment('23:59:59', timeFormat)) ||
    time.isSame(moment('17:00:00', timeFormat)) ||
    time.isSame(moment('23:59:59', timeFormat))
  ) {
    timeCategory = 'NIGHT';
  }

  return timeCategory;
}

回答by Karel Frajták

The example on that page is

该页面上的示例是

moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true

there's no call to formatfunction as in your code so I suggest to try

没有format像您的代码那样调用函数,所以我建议尝试

moment('09:34:00').isBetween('08:34:00', '10:34:00');