Reputation: 31
Hi I am new to javascript I wanted to implement a code where I have two var below start time and end time is military format
var start1= 08:00:00
var endto=03:00:00
I wanted to create a condition where if my giventime is in between the start1
and end to
Ex:
giventime = 02:00:00
it will pass through because it is in the range of 08:00:00 - 03:00:00
giventime = 05:00:00
it will NOT pass through because it is NOT in the range of 08:00:00 - 03:00:00
I tried to used the code below:
var start1= 08:00:00
var endto=03:00:00
var giventime = 02:00:00
if giventime>start1 && giventime<=endto {
//but doesnt work it should allow because 02:00:00 is withing the range of
start1 and endto
}
Upvotes: 1
Views: 304
Reputation: 15247
You need a condition to check if the starting time is higher than the ending one.
In that case, you'll want to check if the given date is greater than the starting one or lesser than the ending time.
"02:00:00" > "08:00:00" || "02:00:00" < "03:00:00" // true
"05:00:00" > "08:00:00" || "05:00:00" < "03:00:00" // false
"02:00:00" > "03:00:00" && "02:00:00" < "08:00:00" // false
"05:00:00" > "03:00:00" && "05:00:00" < "08:00:00" // true
function IsInTime(start, end, given)
{
if (start < end)
{
return (given > start && given <= end);
}
return (given > start || given <= end);
}
var start1 = "08:00:00"
var endto = "03:00:00"
var giventime = "02:00:00"
if (IsInTime(start1, endto, giventime))
{
console.log(giventime + " is in the range " + start1 + " - " + endto);
}
else
{
console.log(giventime + " is NOT in the range " + start1 + " - " + endto);
}
start1 = "08:00:00"
endto = "03:00:00"
giventime = "05:00:00"
if (IsInTime(start1, endto, giventime))
{
console.log(giventime + " is in the range " + start1 + " - " + endto);
}
else
{
console.log(giventime + " is NOT in the range " + start1 + " - " + endto);
}
start1 = "03:00:00"
endto = "08:00:00"
giventime = "02:00:00"
if (IsInTime(start1, endto, giventime))
{
console.log(giventime + " is in the range " + start1 + " - " + endto);
}
else
{
console.log(giventime + " is NOT in the range " + start1 + " - " + endto);
}
start1 = "03:00:00"
endto = "08:00:00"
giventime = "05:00:00"
if (IsInTime(start1, endto, giventime))
{
console.log(giventime + " is in the range " + start1 + " - " + endto);
}
else
{
console.log(giventime + " is NOT in the range " + start1 + " - " + endto);
}
Upvotes: 0
Reputation: 36574
You can add any date with given time and convert it to Date
then use getTime()
to get the miliseconds
. Now check if start > end
then minus 1 day from start
var start1= '08:00:00'
var endto='03:00:00'
var givenTime = '02:00:00'
function isTimeGreater(start,end,given){
start = new Date(`Feb 12 2019 ${start}`).getTime();
end = new Date(`Feb 12 2019 ${end}`).getTime();
given = new Date(`Feb 12 2019 ${given}`).getTime();
if(start > end){
start -= 24*60*60 * 1000;
}
return start < given && given < end;
}
console.log(isTimeGreater(start1,endto,givenTime))
console.log(isTimeGreater(start1,endto,'04:00:00'))
Upvotes: 1
Reputation: 958
Because of the format you're going to have issues. Instead you could look at a time including date this can be done using the new Date()
method.
For instance if you did new Date("January 19, 2019 08:00:00")
for start and similarly did one for end date, you could compare the Date objects with your if
statement.
Upvotes: 0