简体   繁体   English

SQL日期时间值格式

[英]SQL date time value format

i have a SQL field in table 我在表中有一个SQL字段

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 现在,当我尝试输入值时,这就是SQL给我的错误

"sqlMessage": "Incorrect datetime value: 'Sat, 03 Aug 2019' for column 'createdAt' at row 1", “ sqlMessage”:“日期时间值不正确:第1行'createdAt'列的'Sat,03 Aug 2019'”,

This is the value I am passing for createdAt 这是我传递给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 我也尝试过Date.now()但同样的错误

"sqlMessage": "Incorrect datetime value: '1564834662852' for column 'createdAt' at row 1", “ sqlMessage”:“第1行的'createdAt'列的日期时间值不正确:'1564834662852',

This is the first time I am working with SQL, Can someone help me in figuring out what could I be doing wrong? 这是我第一次使用SQL,有人可以帮助我找出我可能做错了什么吗?

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. 如果您不想在表中使用默认时间戳,则可以/应该使用Mysql期望的日期时间格式为YYYY-MM-DD HH:MI:SS datetime格式。 for that you can use 为此,您可以使用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM