Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting ISO date time to zero

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.

like image 593
oneberenjena Avatar asked Jul 20 '26 02:07

oneberenjena


1 Answers

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.

like image 185
Gopalkrishna Narayan Prabhu Avatar answered Jul 21 '26 15:07

Gopalkrishna Narayan Prabhu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!