user1751287
user1751287

Reputation: 501

convert date to UTC ISO midnight

trying to create new date and set it to UTC ISO midnight and get output as following "2017-07-12T00:00:00" but no luck so far.

var dateNow = new Date();
var dateToUTC = dateNow.setUTCHours(0,0,0,0);
/*this gives me an error msg*/
var dateToISO = dateToUTC.toISOString();

Upvotes: 7

Views: 3602

Answers (1)

user27414
user27414

Reputation:

Date.setUTCHours doesn't return a date, it returns a number (see the docs, here) and mutates the date.

You want this:

var dateNow = new Date();
dateNow.setUTCHours(0,0,0,0);
var dateToISO = dateNow.toISOString();

Upvotes: 13

Related Questions