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

5

79

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?

Comrade answered 24/3, 2016 at 9:39 Comment(1)
Check this #23620998Bissell
V
142
  • 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:

const format = 'hh:mm:ss';

// var time = moment() gives you current time. no format required.
const time = moment('09:34:00', format);
const beforeTime = moment('08:34:00', format);
const afterTime = moment('10:34:00', format);

if (time.isBetween(beforeTime, afterTime)) {
  console.log('is between');
} else {
  console.log('is not between');
}
// prints 'is between'
Violetavioletta answered 24/3, 2016 at 9:49 Comment(7)
This seems great! +1 but how would I format the current time as 'hh:mm:ss' without using .format()? So, instead of hardcoding the current time, could I get it via moment?Spaulding
Basically a way to do something like: moment('hh:mm:ss') without actually giving the time, because I want moment to give me that...Spaulding
Ah, in that case just do moment(). you don't have to specify an input format if you don't input a time string. Later, when you would like to actually print out the date in the HTML for the user, you want to give moment an output format, so then you use .format('hh:mm:ss'), but that is the very last step. For everything in between (calculations, comparisons etc. ) a format is not needed.Violetavioletta
Ahhhh! I see! I was overcomplicating it quite a bit! :/ Thanks a lot! :D it works now :)Spaulding
The confusing bit I think is why the author is using hh:mm:ss and not HH:mm:ss as hh:mm:ss is in 12 hour format which does not make sense to me without specifying am and pm.Maximo
it doesn't work for the following instance: var time = moment('21:34:00',format), beforeTime = moment('20:34:00', format), afterTime = moment('01:34:00', format); If it works, please let me know.Alyshaalysia
it doesn't work because your afterTime is before your beforeTime: 01:34 is before 20:34. You of course want 01:34 on the next day, but you didn't define that. So simply do moment('01:34:00', format).add(1, 'day')Violetavioletta
H
5

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;
}
Hamulus answered 4/4, 2018 at 10:10 Comment(2)
it's easier to use moment().format("a") to get the time category. But I know what you wrote above is just an example of the answer to the question.Cardoon
From the docs: "Version 2.13.0 introduces inclusivity. A [ indicates inclusion of a value. A ( indicates exclusion. If the inclusivity parameter is used, both indicators must be passed." So you can eliminate the isSame by using isBetween(first, second, null, '[]');Warmonger
M
2

I have use moment function combination isSameOrAfter and isSameOrBefore instead of isBetween.

if(moment(currentTime,'HH:mm:ss').isSameOrAfter(moment(shift_dayFrom,'HH:mm:ss')) && 
moment(currentTime,'HH:mm:ss').isSameOrAfter(moment(shift_dayTo,'HH:mm:ss')))
My answered 8/1, 2021 at 21:32 Comment(0)
B
1
    const beforeTime = moment(t, "HH:mm");
    const afterTime = moment(t2, "HH:mm");
    
    
             
                
   const time = moment(dueTime, "HH:mm");
    
   if(time.isSameOrAfter(beforeTime) && time.isSameOrBefore(afterTime))
                {
                  
                }
Bi answered 13/11, 2022 at 16:53 Comment(0)
N
-3

The example on that page is

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

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

moment('09:34:00').isBetween('08:34:00', '10:34:00');
Noble answered 24/3, 2016 at 9:48 Comment(1)
He should be passing a parse format string as the second argument (see my answer), otherwise moment will raise a deprecation warning.Violetavioletta

© 2022 - 2024 — McMap. All rights reserved.