[英]How to convert JavaScript Date into PostgreSQL Time (no timezone)?
I use knex with PosgreSQL .我将knex与PosgreSQL一起使用。 I have a table with a Time column.我有一个带有时间列的表。
After trying to insert data in the table, knex
throws an error with the following message:尝试在表中插入数据后, knex
抛出错误并显示以下消息:
...invalid input syntax for type time: \"2021-07-21T14:40:00.000+03:00\..."
Code example代码示例
await knex('table_name')
.insert({ id: 1, time: new Date() })
What is a correct way to preserve JavaScript Date object as a PosgreSQL Time?将 JavaScript 日期 object 保留为 PosgreSQL 时间的正确方法是什么? Should I use 3rd party libs?我应该使用 3rd 方库吗? Or it can be done using knex only?还是只能使用 knex 来完成?
I was able to fix this issue by manually converting the JavaScript Date object into one of the supported formats of the PostgreSQL.我能够通过手动将 JavaScript 日期 object 转换为 PostgreSQL 的支持格式之一来解决此问题。
The 8.5.1.1. Dates
8.5.1.1. Dates
8.5.1.1. Dates
and 8.5.1.2. Times
8.5.1.1. Dates
和8.5.1.2. Times
8.5.1.2. Times
chapters have a full list of supported types. 8.5.1.2. Times
章节有完整的支持类型列表。
My solution was to use date-fns/format
(eg format(new Date(), 'HH:mm') // 14:00
)我的解决方案是使用date-fns/format
(例如format(new Date(), 'HH:mm') // 14:00
)
PS I'm not sure if this approach is right but it works. PS我不确定这种方法是否正确,但它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.