How to convert a MySQL date to JavaScript date?

JavascriptWeb DevelopmentFront End Technology

To convert a MySQL date to JavaScript, use Date.parse.

Example

You can try to run the following code to convert MySQL date to JavaScript date −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var MySQL_date = '2017-12-31 11:55:17';
         document.write("MySQL Date: "+MySQL_date);

         var jsDate = new Date(Date.parse(MySQL_date.replace(/[-]/g,'/')));
         document.write("<br>JavaScript Date: "+jsDate);
      </script>
   </body>
</html>

Output

MySQL Date: 2017-12-31 11:55:17
JavaScript Date: Sun Dec 31 2017 11:55:17 GMT+0530 (India Standard Time)
raja
Published on 14-Feb-2018 12:43:51
Advertisements