Search code examples
javascriptdateconsole

Print True in the console depending on a Specific Date


I am trying to print True in the console when a Specific date comes.

Here is my code :

setInterval(() => {
 if (new Date() == new Date("2022-06-18T06:27:00")) {
   console.log("true");
 } else {
   console.log("false");
 }
}, 1000);

But it keeps showing False

There is another way to do what I am thinking about?

Note: Both of new Date() and the Date which I have provided in the Stamp time are the same on the console


Solution

  • You can use the following:

    setInterval(() => {
     if (new Date().toString() == new Date("2022-06-18T03:32:00").toString()) {
       console.log("true");
     } else {
       console.log("false");
     }
    }, 1000);
    
    

    var date = document.getElementById('date')
        var button = document.getElementById('submit')
        var result = document.getElementById('info')
        var reset = document.getElementById('reset')
    
        button.addEventListener('click', () => {
            date.disabled = true
            button.disabled = true
        var Timer = setInterval(() => {
                if (new Date().toString() == new Date(date.value).toString()) {
                    result.innerHTML = "The date is: " + new Date().toString() + " Which is the same as the date you entered"
                    clearInterval(Timer)
                    date.disabled = false
                    button.disabled = false
                    return;
                }
                result.innerHTML = "Waiting for the date..."
               }, 1000);
        });
    
        reset.addEventListener('click', () => {
            input.disabled = false
            button.disabled = false
            result.innerHTML = "";
        })
    <input type="datetime-local" id="date">
    <button id="submit">Submit</button>
    <button id="reset">Reset</button>
    <p id="info"></p>