[英]NodeJS convert MYSQL Date
我對 nodejs 作為輸出提供的時間格式有點掙扎。
我執行 MYSQL 查詢並返回日期時間(fe 2020-01-08 20:20:25
是我的數據庫中的一個條目)當我從查詢中獲得結果時,輸出看起來像Wed Jan 08 2020 20:20:25 GMT+0100 (GMT+01:00)
我找不到一種方法將它“轉換”回我的數據庫中的格式,你有什么解決方案嗎?
非常感謝。
創建實例時,只需添加使用日期作為字符串的配置
mysql.createConnection({
dateStrings: true
});
我建議使用名為Moment.js 的第三方模塊。 您可以使用npm install moment --save
安裝模塊。 它有助於使用自定義格式進行日期轉換。
下面是我的代碼:
const moment = require('moment'); // Importing the Moment.js module
const date = moment(
"Wed Jan 08 2020 20:20:25 GMT+0100 (GMT+01:00)",
"ddd MMM DD YYYY HH:mm:ss"
); // This is your query from MySQL
let mydate = date.format("YYYY-MM-DD HH:mm:ss"); // Using moment.format() with the given format, it converts the date
console.log(mydate) // Finally outputting the date to the console
以下代碼輸出:2020-01-08 20:20:25
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.