I'm trying to compare a Date comming from a Mongo query that has a date in ISO format. The date is the following:
2018-01-20T00:00:00.000Z
I want to compare today's date with this, so my approach was to create a new date and set its time to zero like this:
var today = new Date();
today.setHours(0,0,0,0); //Sat Jan 20 2018 00:00:00 GMT-0800 (PST)
With this, it seems like the time is set to zero. The problem comes when I convert it to ISO string for comparison:
console.log(today.toISOString()); //2018-01-20T08:00:00.000Z
As you can see, it sets everything to zero but hour, that remains at 08. I can't get to set this hour to zero.
var today = new Date();
today.setUTCHours(0,0,0,0);
document.getElementById('val').innerHTML = today.toISOString();
<label>Date: (ISO String)</label>
<div id="val">
<div>
today.setUTCHours(0,0,0,0);
This will set the UTC hours giving you the desired output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With