PostgreSQL-version 9.4.24
1. 系统当前时间
| select now(); |
| |
| select current_date; |
| |
| select current_time; |
| |
| select current_timestamp; |
| |
| select localtimestamp; |
| |
| select localtime; |
| |
| select clock_timestamp(); |
| |
2. 格式转换
| |
| select to_timestamp(1663740005); |
| |
| select date(to_timestamp(1663743934638::numeric / 1000)); |
| |
| select date(localtimestamp); |
| |
| |
| select to_date('05 Dec 2022', 'DD Mon YYYY'); |
| |
| |
| select to_char(now(), 'YYYY-MM-DD HH:MI:SS'); |
| |
| |
| select timestamp '2022-09-29 10:23:18'; |
| |
| select date '2022-09-29 10:23:18'; |
| |
| select time '2022-09-29 10:23:18'; |
| |
| select timestamp with time zone '2022-09-29 10:23:18'; |
| |
| select timestamp 'epoch' + 1663740005 * interval '1 second'; |
3. 时间计算
| select now() + interval '2 day'; |
| select now() + interval '2 days'; |
| select now() + interval '2 hour'; |
| select now() + interval '2 minute'; |
| select now() + interval '2 second'; |
| |
| |
| select age('2022-09-21', '2022-09-29'); |
| |
| select age(date '2022-09-21'); |
| |
interval部分也可以不写,其后的值有以下形式
Abbreviation |
Meaning |
Y |
Years |
M |
Months (in the date part) |
W |
Weeks |
D |
Days |
H |
Hours |
M |
Minutes (in the time part) |
S |
Seconds |
4. 时间截取
| -- select date_part(text, timestamp) |
| select date_part('year' , now()); |
| |
| -- select extract(field from timestamp) |
| select extract(year from now()); |
| |
| -- select DATE_TRUNC(text, timestamp) |
| select date_trunc('hour', now()); |
| select date_trunc('day', timestamp '2022-09-28 14:25:01'); |
| |
参考:
http://postgres.cn/docs/9.4/functions-datetime.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)