Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment JS compare hours and minutes

I'm mapping through array that has data like: ['00:00', '00:30', '01:00'] and all way long to 23:30.

Each of array element is to be compared with the time of Open and Close times, which are like open '00:15', close '22:00' etc.

According to comparison I need to disable or enable elements on page: if open and close in range - elements are available, otherwise - disabled. So the question is how to compare these hours and minutes via moment JS?

like image 507
NeedHate Avatar asked Nov 19 '25 21:11

NeedHate


1 Answers

Moment makes this fairly simple:

var times = ['00:00', '00:30', '01:00']

var parseTime = timeString => moment(timeString, 'HH:mm')

var closingTime = parseTime('00:00')

var pastClosing = times.map(time => parseTime(time).isAfter(closingTime));

console.dir(pastClosing)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/locale/en-gb.js"></script>
like image 149
OliverRadini Avatar answered Nov 22 '25 10:11

OliverRadini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!