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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With