gpt4 book ai didi

django - PostgreSQL 日期格式

转载 作者:行者123 更新时间:2023-11-29 14:05:32 25 4
gpt4 key购买 nike

我有一个 Web 应用程序(用 python/django 编写)(由于规范错误)Web 表单需要“YYYY-mm-dd”日期格式,而其他应用程序使用“dd/mm/yy”日期格式。

有没有办法告诉 postgresql 接受两种格式的日期?例如,尝试“dd/mm/yy”,如果失败,则尝试“yyyy-mm-dd”。

那太棒了。

最佳答案

来自fine manual :

Date and time input is accepted in almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES, and others. For some formats, ordering of day, month, and year in date input is ambiguous and there is support for specifying the expected ordering of these fields. Set the DateStyle parameter to MDY to select month-day-year interpretation, DMY to select day-month-year interpretation, or YMD to select year-month-day interpretation.

PostgreSQL is more flexible in handling date/time input than the SQL standard requires. See Appendix B for the exact parsing rules of date/time input and for the recognized text fields including months, days of the week, and time zones.

因此 PostgreSQL 应该能够处理几乎任何您输入的日期格式。但是,您的“dd/mm/yy”格式不明确。但是,有 DateStyle配置参数来帮助解决这种歧义。

例如:

=> create table x (d date not null);
=> insert into x values ('2001-01-10');
=> insert into x values ('Feb 2 2980');
=> insert into x values ('01/02/03');
=> select * from x;
d
------------
2001-01-10
2980-02-02
2003-02-01

也就是说,我建议在内部将所有内容移至 ISO 8601 (YYYY-MM-DD) 并在应用程序的边缘处理转换。 OTOH,要面对现实,所以你应该做任何你必须做的事情来实现它。

关于django - PostgreSQL 日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5467031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com