I need to check if my current time lies between any of Switch-1 or Switch-2 or Switch-3. I tried in for-loop and matched conditions but somehow it is not working, I am not sure if it is due to string comparison.
Example :
- If current time is 23-09-2020 7:45 then want to print 23-09-2020 Switch-1
- If current time is 23-09-2020 18:40 then want to print 23-09-2020 Switch-2
- If my current time is 24-09-2020 03:00 then want to print 23-09-2020 Switch-3 (current Date minus 1 Day)
i.e. Special case of time between 00:00 to 05:00 as date is changed to 24th Sep, I want current Date minus 1 Day i.e. 23rd Sep.
var Data;
var m = moment().format('HH:mm')
console.log(m)
var Time = [
{
"switchName": 'switch 3',
"start": '22:00',
"end": '05:00'
},
{
"switchName": 'switch 1',
"start": '05:00',
"end": '15:00',
},
{
"switchName": 'switch 2',
"start": '15:00',
"end": '22:00',
},
]
for (let i = 0; i < Time.length; i++) {
const start = Time[i].start;
const end = Time[i].end;
if (start > end) {
if (m <= start || m >= end) {
data = {
currentdate: moment().format('YYYY-MM-DD'),
currentswitch: Time[i].switchName
}
console.log(data);
break;
}
}
else {
if (m >= start && m <= end) {
data = { currentdate: moment().format('YYYY-MM-DD'), currentswitch: Time[i].switchName }
console.log(data);
break;
}
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>