Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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',     afterTime = '10:34:00'; 

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

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

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.

How do I get this to work?

like image 358
ᔕᖺᘎᕊ Avatar asked Mar 24 '16 09:03

ᔕᖺᘎᕊ


People also ask

How do you find the time between two times a moment?

js using the isBetween() function that checks if a moment is between two other moments, optionally looking at unit scale (minutes, hours, days, etc). Syntax: moment(). isBetween(moment-like, moment-like); moment().

How do you know if a date is between two dates using a moment?

We can use the isBetween method to check if a date is between 2 dates. We create the compareDate moment object that we want to check if it's between startDate and endDate . To do the comparison, we call isBetween on compareDate with startDate and endDate as the arguments.

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().

Why you shouldnt use MomentJS?

However, Moment. js has too many drawbacks compared to modern date and time libraries. Its API is not immutable, it is large and it doesn't support tree shaking. Even the Moment team discourages to use their library in new projects.


2 Answers

  • 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' 
like image 62
Linus Borg Avatar answered Sep 28 '22 02:09

Linus Borg


I had to use isBetween and isSame in my case to cover the before and after time I used in the isBetween condition.

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; } 
like image 29
Woppi Avatar answered Sep 28 '22 03:09

Woppi