Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Convert MySQL DATETIME Value to JSON Format in JavaScript



To convert, use JSON.stringify(). Following is the code to convert MySQL DATETIME value to JSON format in JavaScript −

<script>
var mySQLDateTime = new Date("Fri Sep 06 2019 22 −54 −48 ");
var yearValue = mySQLDateTime.getFullYear();
var dateValue = mySQLDateTime.getDate();
var monthValue=mySQLDateTime.getMonth();
var hour=mySQLDateTime.getHours();
var minutes=mySQLDateTime.getMinutes();
var second=mySQLDateTime.getSeconds();
jsonObject={"year" −yearValue,"month" :monthValue,"DateValue" :dateValue,"Hour" :hour ,"Minutes" :minutes,"Second" :second};
var dateJsonObject = JSON.stringify(jsonObject);
document.write(dateJsonObject);
</script>

The screenshot of the code is as follows −

This will produce the following output −

{"year" :2019,"month" :8,"DateValue" :6,"Hour" :22,"Minutes" :54,"Second" :48}

The snapshot of the output is as follows −

Updated on: 2019-10-07T10:49:22+05:30

612 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements