将日期字符串转换为与 postgresql 兼容的日期时间

Bhi*_*sen 5 django postgresql datetime python-2.7

我有一个格式为的日期字符串,

"dd/mm/yyyy"
e.g="23/2/2017"
Run Code Online (Sandbox Code Playgroud)

如何将其转换为有效格式,以便我可以将此值保存在 postgresql 的 Datetime 字段中。

我尝试使用 datetime 包,但没有得到。

Vao*_*sun 9

在 Postgres 中你可以“调整”你的日期样式,例如:

t=# set datestyle TO DMY;
SET
Time: 0.215 ms
t=# select '23/2/2017'::timestamptz;
      timestamptz
------------------------
 2017-02-23 00:00:00+00
(1 row)
Run Code Online (Sandbox Code Playgroud)

或者只是用正确的掩码“解析”(忽略不合适的日期样式),例如:

t=# select to_timestamp('23/2/2017','DD/MM/YYYY');
      to_timestamp
------------------------
 2017-02-23 00:00:00+00
(1 row)
Run Code Online (Sandbox Code Playgroud)