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\',
afterT
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;
}
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');
isBetween()
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'