简体   繁体   English

如何将 JavaScript 日期转换为 PostgreSQL 时间(无时区)?

[英]How to convert JavaScript Date into PostgreSQL Time (no timezone)?

I use knex with PosgreSQL .我将knexPosgreSQL一起使用。 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. Dates8.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.

相关问题 如何在JavaScript中将没有时区的PostgreSQL时间转换为日期? - How to convert PostgreSQL time without time zone to date in JavaScript? 使用JavaScript将带有时区的日期字符串转换为本地时间的日期对象 - Use JavaScript to convert a date string with timezone to a date object in local time 如何根据新的时区转换javascript日期 - how to convert javascript date according to a new timezone 如何将JavaScript日期转换为特定时区 - How to convert javascript date to a particular timezone 将(日期和时间戳从 postgresql)转换为 javascript 时间 - convert (date and timestamp from postgresql) to javascript time 如何将在纽约时区保存的日期时间转换为javascript中的本地时区? - How to convert date time saved in New York timezone to local time zone in javascript? 如何在 Javascript 中将 PST 时区日期转换为本地时区? - How to convert PST timezone date to Local TimeZone in Javascript? JavaScript 如何将欧洲/柏林时区转换为本地时区日期 - JavaScript how to convert Europe/Berlin timezone to local timezone date Momentjs:如何将一个时区的日期/时间转换为 UTC 日期/时间 - Momentjs: How to convert date/time of one timezone to UTC date/time 如何在JavaScript中将带有时区的时间转换为UTC - How to convert time with a timezone to UTC in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM