如何在 JavaScript 中将 MySQL DATETIME 值转换为 JSON 格式?
mysqlmysqli databasejavascriptjson
要进行转换,请使用 JSON.stringify()。以下是在 JavaScript 中将 MySQL DATETIME 值转换为 JSON 格式的代码−
<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>
代码截图如下 −
这将产生以下输出 −
{"year" :2019,"month" :8,"DateValue" :6,"Hour" :22,"Minutes" :54,"Second" :48}
输出的快照如下 −