How to convert a string in to date object in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

To convert a string in to date object Date() method should be used. This method creates a date instance that represents a single moment in time in a platform-independent format.

Example

In the following example a string named "str" is initially parsed using JSON.parse() method and then converted in to date object using Date() method.

Live Demo 

<html>
<body>
<script>
   var str = '{"name":"Ram", "DOB":"1980-11-1", "country":"India"}';
   var dateObj = JSON.parse(str);
   dateObj.DOB = new Date(dateObj.DOB);
   document.write(dateObj.DOB);
</script>
</body>
</html>

Output

Thu Nov 01 0198 00:00:00 GMT+0553 (India Standard Time)
raja
Published on 27-Jun-2019 19:25:15
Advertisements