Reputation: 71
This is my NodeJs API that is connected to my MySQL workbench
var express = require('express');
const bodyParser = require('body-parser')
var cors = require('cors');
const app = express();
const mysql = require("mysql");
app.use(express.json());
app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "12112002suman",
database:"chutiyadb",
});
// app.get('/' , (req,res) => {
// res.send("connected to database");
// });
db.connect(function(err){
if(err) throw err
});
function insertData(Title,Amount,Date){
db.query('INSERT INTO expenseitems (title, amount , date) VALUES ( ? , ? , ?)',
[Title, Amount, Date], function (err, result) {
if (err) throw err;
});
}
app.post('/api/insert', function(req,res) {
insertData(req.body.title, req.body.amount, req.body.date);
});
app.listen(3001, () => {
console.log(" running on port 3001");
});
I'm trying to take Date input by using calender format from a user that is
08-06-2022 as dd-mm-yyyy
Error is
"Incorrect date value: '2022-06-11T00:00:00.000Z' for column 'date' at row 1"
Upvotes: 2
Views: 3062
Reputation: 315
Convert Your current date format into your required date format using the moment library.
first, install the moment library
npm i moment
then, convert the date format
let current_date = moment('Your date input').format("YYYY-MM-DD hh:mm:ss - your required date format");
Upvotes: 2