Reputation: 17
I'm struggling a bit with the timeformat that nodejs provides as output.
I do a MYSQL query and get a datetime back (f.e 2020-01-08 20:20:25
is a entry in my DB)
When I get the result from my query the output looks like Wed Jan 08 2020 20:20:25 GMT+0100 (GMT+01:00)
I couldn't find a method to "convert" it back to the format from my database, do you have any solution for this?
Thank you very much.
Upvotes: 1
Views: 4821
Reputation: 547
I recommend using a third party module named Moment.js. You can use npm install moment --save
to install the module. It helps with date conversion with custom formats.
Below is my code:
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
The following code outputs: 2020-01-08 20:20:25
Upvotes: 2
Reputation: 41
when you create the instance just add the config for use dates as strings
mysql.createConnection({
dateStrings: true
});
Upvotes: 4