anonymous
anonymous

Reputation: 61

javascript toISOString return incorrect date issue

The toISOString method return incorrect date.

 // Input 
    var a = new Date("June 08, 2018");
    a.toISOString().slice(0, 10);

 // Output
    "2018-06-07"

// Expected output
   "2018-06-08"

Upvotes: 2

Views: 2664

Answers (1)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Use Z at the end of your date value. This will ignore the ISO timezone conversion and it will give you the value based on your local time.

var a = new Date("June 08, 2018 Z");
var res = a.toISOString().slice(0, 10);
console.log(res);

Upvotes: 9

Related Questions