当前位置:网站首页>How to use Moment. JS to check whether the current time is between 2 times

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

2022-07-06 14:48:00 javail

problem :

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

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

How do I use Moment.JS to check whether the current time is between beforeTime and afterTime ? How to use Moment.JS Check whether the current time is beforeTime and afterTime Between ?

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

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. But it doesn't work , Because once I will be the first ( current time ) The time is formatted as a string , It is no longer an object of time .I've also tried using: I've also tried to use :

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. But I got false , Because when I format the current time again , It is no longer a moment .

How do I get this to work? How can I make this work ?


Solution :

Reference resources : https://stackoom.com/en/question/2RsV5
原网站

版权声明
本文为[javail]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131347463903.html

随机推荐