0

Here is the String example I am trying to convert to Date `

let sdate = "2008-08-17T07:24:03.000Z";
let xdate = Date(sdate);
console.log(xdate);
`

Am I using Date wrong? How should I convert dateformat of any String to Date? Why does Date return today's date instead of given date?

AK-35
  • 559
  • 2
  • 9
  • 24
  • 1
    Does this answer your question [enter link description here](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) ? – Nikhil Unni Dec 22 '20 at 07:47
  • 1
    When called as a function, Date returns a string for the current date and time, ignoring any arguments passed to it. When called as a constructor, it creates a new Date object based on the arguments passed (if any). If you want to parse a string to a date, then call it as a constructor with *new*: `new Date(sdate)`. – RobG Dec 22 '20 at 08:15

1 Answers1

4

Use new Date constructor.

let sdate = "2008-08-17T07:24:03.000Z";
let xdate = new Date(sdate);
console.log(xdate);
Kevin Zhang
  • 1,012
  • 6
  • 14