Alwaysblue
Alwaysblue

Reputation: 11830

SQL date time value format

i have a SQL field in table

which looks like this

 `updatedAt` datetime(6) NOT NULL ON UPDATE CURRENT_TIMESTAMP(6),
  `createdAt` datetime(6) NOT NULL,

Now, When I am trying to enter the value this is what error SQL is giving me

"sqlMessage": "Incorrect datetime value: 'Sat, 03 Aug 2019' for column 'createdAt' at row 1",

This is the value I am passing for createdAt

'Sat, 03 Aug 2019'

created using this

const dateString = new Date().toUTCString().split(' ').slice(0, 4).join(' ');

I have also tried Date.now() but same error

"sqlMessage": "Incorrect datetime value: '1564834662852' for column 'createdAt' at row 1",

This is the first time I am working with SQL, Can someone help me in figuring out what could I be doing wrong?

Upvotes: 0

Views: 246

Answers (1)

Mukesh Chandra
Mukesh Chandra

Reputation: 56

you can just update your schema for setting default datetime

createdAt DATETIME DEFAULT CURRENT_TIMESTAMP, updatedAt DATETIME ON UPDATE CURRENT_TIMESTAMP 

or

if you don't want to use default timestamps in your table you can / should datetime format in your date in YYYY-MM-DD HH:MI:SS format which is expected by Mysql. for that you can use

const dateString = new Date().toISOString().substring(0,19).replace('T',' ');

output: "2019-08-03 13:19:41"

Upvotes: 2

Related Questions