简体   繁体   中英

JavaScript if-statement with AND and dates

I have a statement like this:

 // this is a date I select from input field and set its time to 12AM var date = new Date(new Date().setHours(0, 0, 0, 0)); console.log(date); var currentDate = new Date(); console.log(currentDate); if (date.getTime() === new Date(currentDate.setHours(0, 0, 0, 0)).getTime() && currentDate.getTime() > (new Date(currentDate.setHours(11,30,0,0)).getTime())) { console.log("It works!!!"); } else{ console.log("That didn't work as expected :("); } 

And I don't know why it is not working. when I check each comparison in IF statement separately it works but with && it's not.

What can be a problem here?

 var date = new Date(new Date().setHours(0, 0, 0, 0)); console.log(date); var currentDate = new Date(); console.log(currentDate); if (date.getTime() === new Date(currentDate.setHours(0, 0, 0, 0)).getTime() && currentDate.getTime() > (new Date(currentDate.setHours(11,30,0,0)).getTime())) { console.log("It works!!!"); } else{ console.log("That didn't work as expected :("); } console.log(date.getTime() === new Date(currentDate.setHours(0, 0, 0, 0)).getTime()); console.log(currentDate.getTime() > (new Date(currentDate.setHours(11,30,0,0)).getTime())); 

That`s why, "That didn't work as expected :("

At last, after some interesting debugging, I managed to find your issue.

The line,

date.getTime() === new Date(currentDate.setHours(0, 0, 0, 0)).getTime() in your first condition is changing the currentDate variable you have taken above, using the code currentDate.setHours(0, 0, 0, 0) , which is making the second conition to fail.

So, if you take them both it is not working,

Here is an example of the not working snippet.

 var date = new Date(new Date().setHours(0, 0, 0, 0)); console.log(date); var currentDate = new Date(); if(date.getTime() === new Date(currentDate.setHours(0, 0, 0, 0)).getTime() && currentDate.getTime() > (new Date(currentDate.setHours(11,30,0,0)).getTime())) { console.log("It works!!!"); } else{ console.log("That didn't work as expected :("); } 

Now, check the working snippet

  // this is a date I select from input field and set its time to 12AM var date = new Date(new Date().setHours(0, 0, 0, 0)); console.log(date); var currentDate = new Date(); if(date.getTime() === new Date(new Date().setHours(0, 0, 0, 0)).getTime() && currentDate.getTime() > (new Date(currentDate.setHours(11,30,0,0)).getTime())) { console.log("It works!!!"); } else{ console.log("That didn't work as expected :("); } 

I have taken,

new Date().setHours(0, 0, 0, 0)).getTime() instead of

currentDate.setHours(0, 0, 0, 0)).getTime() so that, currentDate is not modified in the first condition.

How about this:

 var date = new Date(); var today1130 = new Date(new Date().setHours(11,30)); console.log("input date is today: ", date.toDateString() === today1130.toDateString()); console.log("input date is after today 11:30: ", date > today1130); 

I think the reason why it didn't work is because there's a miscalculates for times between 11:30am and 12:00pm. Try this and let me know :)

var todaysNoon = new Date();
var nextNoon = new Date();

todaysNoon.setHours(11,30,0,0);
if (todaysNoon <= nextNoon){ nextNoon.setDate(nextNoon.getDate()+1); }
nextNoon.setHours(11,30,0,0);

if (todaysNoon.getTime() <= new Date(nextNoon.setHours(0, 0, 0, 0)).getTime() && todaysNoon.getTime() <= new Date(nextNoon.setHours(0, 0, 0, 0)).getTime()) {
    console.log("It works!!!");
} else {
    console.log("That didn't work as expected :(");
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM