你得到00:00:00 作为时间部分可能是因为有人只插入了没有时间部分的日期。查看结果:
select current_date, current_timestamp;
你会得到:
"2013-03-07";"2013-03-07 08:31:59.098661+01"
当您仅使用日期插入或更新时间戳列时,时间部分将被剪切为00:00:00。如果你想要日期、时间和时区,你应该使用current_timestamp。
在服务器端和客户端都可以将此类日期转换为字符串。有很多PostgreSQL datetime functions、PostgreSQL format functions可以和Python函数一样使用。
与:
select to_char(current_timestamp, 'DD.MM.YY HH12:MI:SS')
你会得到类似的东西:
"07.03.13 08:40:50"
Lafada 回答涵盖了日期时间对象的 Python 格式。
当您想显示现在和某个日期时间之间的差异时,您可以使用 PostgreSQL age() 函数:
select age(timestamp '2003-03-01');
结果:
"10 years 6 days"
当然 Python 也可以显示时间戳之间的差异:
import datetime
dt_old = datetime.datetime.strptime('2001-12-31 22:33:44', '%Y-%m-%d %H:%M:%S')
dt_diff = (datetime.datetime.today() - dt_old)
print(dt_diff)
我的结果:
4083 days, 10:16:14.140000