Skip to content
Advertisement

syntax error unexpected toke in if statement in js

I am receiving a syntax error with unexpected token < at line 14 when i try to run this script in my browser. What i am trying to do is open my classes 5 minutes before class and still open when ran all the way up to 5 minutes before the next class.

<script>
//Current time
var date = new Date();
var time = date.getTime();

//Time checker
if(time >= 7:55 && < 8:55){
    window.open('https://classroom.google.com/c/MTIyMjc3NTE0MzEw');
}
if(time >= 8:55 && < 9:55){
    window.open('https://classroom.google.com/c/MTE1MjA4MzM5MDgz');
}
if(time >= 9:55 && < 10:55){
    window.open('https://classroom.google.com/c/MTIyMjk4MzAxMjYx');
}
if(time >= 10:55 && < 11:55){
    window.open('https://classroom.google.com/c/MTIzMjMyNzU4ODk2');
}
if(time >= 11:55 && < 12:55){
    window.open('https://classroom.google.com/c/MTIzMTkzMjU1MjAx');
}
if(time >= 12:55 && < 13:55){
    window.open('https://classroom.google.com/c/MTIyMjk4MzAxMjQx');
}
if(time >= 13:55 && < 14:55){
    window.open('https://classroom.google.com/c/MTIyNDk3Mjk5NDQ2');
}
if(time >= 14:55 && <= 15:00){
    window.open('https://classroom.google.com/c/MTIyNjk1NTQxMzYw');
}
</script>

Advertisement

Answer

date.getTime() doesn’t return the time of day. If you want the time of day, use date.getHours() and date.getMinutes(). You can then convert this to the number of minutes since midnight, which you can use to tell if the time is within your specific ranges.

//Current time
var date = new Date();
var time = 60 * date.getHours() + date.getMinutes();

//Time checker
if(time >= (7 * 60 + 55) && time < (8 * 60 + 55)){
    window.open('https://classroom.google.com/c/MTIyMjc3NTE0MzEw');
}
if(time >= (8 * 60 + 55) && time < (9 * 60 + 55)){
    window.open('https://classroom.google.com/c/MTE1MjA4MzM5MDgz');
}
if(time >= (9 * 60 + 55) && time < (10 * 60 + 55)){
    window.open('https://classroom.google.com/c/MTIyMjk4MzAxMjYx');
}
if(time >= (10 * 60 + 55) && time < (11 * 60 + 55)){
    window.open('https://classroom.google.com/c/MTIzMjMyNzU4ODk2');
}
if(time >= (11 * 60 + 55) && time < (12 * 60 + 55)){
    window.open('https://classroom.google.com/c/MTIzMTkzMjU1MjAx');
}
if(time >= (12 * 60 + 55) && time < (13 * 60 + 55)){
    window.open('https://classroom.google.com/c/MTIyMjk4MzAxMjQx');
}
if(time >= (13 * 60 + 55) && time < (14 * 60 + 55)){
    window.open('https://classroom.google.com/c/MTIyNDk3Mjk5NDQ2');
}
if(time >= (14 * 60 + 55) && time < (15 * 60 + 55)){
    window.open('https://classroom.google.com/c/MTIyNjk1NTQxMzYw');
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement