How to check if current time is between the time stored in Array

Viewed 127

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 :

  1. If current time is 23-09-2020 7:45 then want to print 23-09-2020 Switch-1
  2. If current time is 23-09-2020 18:40 then want to print 23-09-2020 Switch-2
  3. 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>

1 Answers

If start > end (i.e. the case of "switch 3"), your test is incorrect: (m <= start || m >= end). It should be: if (m >= start || m <= end) (meaning 22-23 or 0-5).

You also wrote: "If my current time is 03:00 then want to print 23-09-2020 Switch-3 (current Date minus 1)". There are two problems here:

  • You said you want the date in DD-MM-YYYY format but you are using format('YYYY-MM-DD'). You'll have to decide what you want and change the format accordingly.

  • You said you want "current Date minus 1" but it's not clear what "minus 1" means. Minus one second? Minute? Hour? Day? Month? Year? Assuming you mean "minus 1 day", you can use moment().subtract(1, "days") to subtract 1 day before formatting, so your final code would be:

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 = {
                                // Subtract 1 day:
                                currentdate: moment().subtract(1, "days").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;
                }
        }
}
Related