简体   繁体   English

如何检查混合日期格式的月和日顺序?

[英]How can I check month and day order in mixed date format?

There's column which contains date in different formats (ie. YYYY-MM-DD or YYYY-DD-MM ).有一列包含不同格式的日期(即YYYY-MM-DDYYYY-DD-MM )。

When I query it with format time:date, it throw an error: date/time field value out of range: "2022-23-02"当我以时间格式查询它时:日期,它抛出错误:日期/时间字段值超出范围:“2022-23-02”

How can i solve it?我该如何解决?

How can I check if it's YYYY-MM-DD or YYYY-DD-MM or another?我如何检查它是 YYYY-MM-DD 还是 YYYY-DD-MM 或其他?

If you have no field that tells you whether it's YYYY-DD-MM or YYYY-MM-DD, you are kinda out of luck because it's possible that a value could be valid in both formats.如果您没有字段告诉您它是 YYYY-DD-MM 还是 YYYY-MM-DD,那么您有点不走运,因为一个值可能在两种格式中都有效。

If you are able to redesign, and you must store the date as a string then use YYYY-MM-DD as it's easier for sorting.如果您能够重新设计,并且必须将日期存储为字符串,则使用 YYYY-MM-DD,因为它更容易排序。 Optimally, just pass a JavaScript Date object to field of type date , timestamp , or timestampz to the database driver or orm and let it handle the conversion for you.最佳情况下,只需将 JavaScript 日期 object 传递给类型为datetimestamptimestampz的字段到数据库驱动程序或 orm 并让它为您处理转换。 Here's an example with node-postgres: https://node-postgres.com/features/types (the section labeled "date / timestamp / timestamptz")下面是 node-postgres 的示例: https://node-postgres.com/features/types (标记为“date / timestamp / timestamptz”的部分)

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

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