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


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)

Updated on: 29-Jun-2020

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements