Sharko Ots
Sharko Ots

Reputation: 3

i think && don't work in javascript for me

This is my code:

var txt = document.getElementById("HRS");
var dte = new Date();
var DayNum = dte.getDay();
const min = dte.getMinutes().toString().replace(/\d+/g, (match, offset,
    string) => match < 10 ? '0' + match : match);
const hours = dte.getHours().toString().replace(/\d+/g, (match, offset,
    string) => match < 10 ? '0' + match : match);
if (DayNum == 1) {
    if (hours < 07) {
        txt.innerHTML = "Ouvre à 7h30.";
        txt.style.color = "#F4524D";
    } else if (hours == 07 && min < 30) {
        txt.innerHTML = "Ouvre à 7h30.";
        txt.style.color = "#F4524D";
    } else if (hours == 07 && min > 29) {
        txt.innerHTML = "Ouvert jusqu'à 19h.";
        txt.style.color = "#4CF470";
    } else if (hours > 07 && hours < 19) {
        txt.innerHTML = "Ouvert jusqu'à 19h.";
        txt.style.color = "#4CF470";
    } else if (hours > 19) {
        txt.innerHTML = "Ouvre demain à 7h30.";
        txt.style.color = "#F4524D";
    }
} else {
    txt.innerHTML = "fermé";
}

When i remove all && the code work.

This code is placed in a html file. (between script> /script> and in window.onload function) This is a Javascript code

Upvotes: 0

Views: 36

Answers (1)

Asons
Asons

Reputation: 87191

The main problem is here

if (hours < 07) {...}

which won't work as you make the number/date being of type string here

const hours = dte.getHours().toString()...
const min = dte.getMinutes().toString()...

and with that compare the string value in hours with the number 07, and the same goes for the string value in min.


Either convert hours/min back to number or quote your numbers so they becomes string

if (hours < "07") {...}

As commented, if to keep it all as numbers, change your 07 to 7.

Upvotes: 1

Related Questions