ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
随笔-102  文章-0  评论-83  # [PostgreSQL的时间/日期函数使用](http://www.cnblogs.com/mchina/archive/2013/04/15/3010418.html) PostgreSQL的常用时间函数使用整理如下: **一、获取系统时间函数** 1.1 获取当前完整时间 select now(); [![](https://box.kancloud.cn/2015-10-30_5632e1be47f9c.gif)]( "复制代码") ~~~ david=# select now(); now ------------------------------- 2013-04-12 15:39:40.399711+08 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1be5782c.gif)]( "复制代码") current_timestamp 同 now() 函数等效。 [![](https://box.kancloud.cn/2015-10-30_5632e1be65e94.gif)]( "复制代码") ~~~ david=# select current_timestamp; now ------------------------------- 2013-04-12 15:40:22.398709+08 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1be752d7.gif)]( "复制代码") 1.2 获取当前日期 select current_date; [![](https://box.kancloud.cn/2015-10-30_5632e1be821f1.gif)]( "复制代码") ~~~ david=# select current_date; date ------------ 2013-04-12 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1be916c2.gif)]( "复制代码") 1.3 获取当前时间 select current_time; [![](https://box.kancloud.cn/2015-10-30_5632e1be9d0a7.gif)]( "复制代码") ~~~ david=# select current_time; timetz -------------------- 15:43:31.101726+08 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1beaa407.gif)]( "复制代码") **二、时间的计算** [![](https://box.kancloud.cn/2015-10-30_5632e1beb5aa7.gif)]( "复制代码") ~~~ david=# select now(); now ------------------------------- 2013-04-12 15:47:13.244721+08 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bec4be4.gif)]( "复制代码") 2.1 两年后 [![](https://box.kancloud.cn/2015-10-30_5632e1bed7bee.gif)]( "复制代码") ~~~ david=# select now() + interval '2 years'; ?column? ------------------------------- 2015-04-12 15:49:03.168851+08 (1 row) david=# select now() + interval '2 year'; ?column? ------------------------------- 2015-04-12 15:49:12.378727+08 (1 row) david=# select now() + interval '2 y'; ?column? ------------------------------ 2015-04-12 15:49:25.46986+08 (1 row) david=# select now() + interval '2 Y'; ?column? ------------------------------- 2015-04-12 15:49:28.410853+08 (1 row) david=# select now() + interval '2Y'; ?column? ------------------------------- 2015-04-12 15:49:31.122831+08 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bee5625.gif)]( "复制代码") 2.2 一个月后 [![](https://box.kancloud.cn/2015-10-30_5632e1bef2dde.gif)]( "复制代码") ~~~ david=# select now() + interval '1 month'; ?column? ------------------------------ 2013-05-12 15:51:22.24373+08 (1 row) david=# select now() + interval 'one month'; ERROR: invalid input syntax for type interval: "one month" LINE 1: select now() + interval 'one month'; ^ david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bf0b9ad.gif)]( "复制代码") 2.3 三周前 [![](https://box.kancloud.cn/2015-10-30_5632e1bf19889.gif)]( "复制代码") ~~~ david=# select now() - interval '3 week'; ?column? ------------------------------- 2013-03-22 16:00:04.203735+08 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bf26e82.gif)]( "复制代码") 2.4 十分钟后 [![](https://box.kancloud.cn/2015-10-30_5632e1bf353ee.gif)]( "复制代码") ~~~ david=# select now() + '10 min'; ?column? ------------------------------- 2013-04-12 16:12:47.445744+08 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bf42494.gif)]( "复制代码") **说明:** 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 |           2.5 计算两个时间差 使用 age(timestamp, timestamp) [![](https://box.kancloud.cn/2015-10-30_5632e1bf4cf57.gif)]( "复制代码") ~~~ david=# select age(now(), timestamp '1989-02-05'); age ---------------------------------------- 24 years 2 mons 7 days 17:05:49.119848 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bf5809e.gif)]( "复制代码") [![](https://box.kancloud.cn/2015-10-30_5632e1bf67ac8.gif)]( "复制代码") ~~~ david=# select age(timestamp '2007-09-15'); age ------------------------ 5 years 6 mons 27 days (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bf7692f.gif)]( "复制代码") **三、时间字段的截取** 在开发过程中,经常要取日期的年,月,日,小时等值,PostgreSQL 提供一个非常便利的EXTRACT函数。 ~~~ EXTRACT(field FROM source) ~~~ field 表示取的时间对象,source 表示取的日期来源,类型为 timestamp、time 或 interval。 3.1 取年份 [![](https://box.kancloud.cn/2015-10-30_5632e1bf82b49.gif)]( "复制代码") ~~~ david=# select extract(year from now()); date_part ----------- 2013 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bf90ff4.gif)]( "复制代码") 3.2 取月份 [![](https://box.kancloud.cn/2015-10-30_5632e1bfa0b73.gif)]( "复制代码") ~~~ david=# select extract(month from now()); date_part ----------- 4 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bfad886.gif)]( "复制代码") [![](https://box.kancloud.cn/2015-10-30_5632e1bfbbb1a.gif)]( "复制代码") ~~~ david=# select extract(day from timestamp '2013-04-13'); date_part ----------- 13 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bfc9cc6.gif)]( "复制代码") [![](https://box.kancloud.cn/2015-10-30_5632e1bfd8513.gif)]( "复制代码") ~~~ david=# SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute'); date_part ----------- 40 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1bfe5835.gif)]( "复制代码") 3.3 查看今天是一年中的第几天 [![](https://box.kancloud.cn/2015-10-30_5632e1bff105c.gif)]( "复制代码") ~~~ david=# select extract(doy from now()); date_part ----------- 102 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1c00a57f.gif)]( "复制代码") 3.4 查看现在距1970-01-01 00:00:00 UTC 的秒数 [![](https://box.kancloud.cn/2015-10-30_5632e1c016fbc.gif)]( "复制代码") ~~~ david=# select extract(epoch from now()); date_part ------------------ 1365755907.94474 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1c023999.gif)]( "复制代码") 3.5 把epoch 值转换回时间戳 [![](https://box.kancloud.cn/2015-10-30_5632e1c0304eb.gif)]( "复制代码") ~~~ david=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1369755555 * INTERVAL '1 second'; ?column? ------------------------ 2013-05-28 23:39:15+08 (1 row) david=# ~~~ [![](https://box.kancloud.cn/2015-10-30_5632e1c03f0c7.gif)]( "复制代码") 以上是基本的PG时间/日期函数使用,可满足一般的开发运维应用。   详细用法请参考: PostgreSQL官方说明:[http://www.postgresql.org/docs/9.2/static/functions-datetime.html](http://www.postgresql.org/docs/9.2/static/functions-datetime.html) 分类: [Postgresql](http://www.cnblogs.com/mchina/category/381458.html) 标签: [postgresql](http://www.cnblogs.com/mchina/tag/postgresql/), [时间函数](http://www.cnblogs.com/mchina/tag/时间函数/), [日期函数](http://www.cnblogs.com/mchina/tag/日期函数/) 绿色通道: [好文要顶]()[关注我]()[收藏该文]()[与我联系](http://space.cnblogs.com/msg/send/David_Tang)[![](https://box.kancloud.cn/2015-10-30_5632e1c04a078.png)]( "分享至新浪微博") [![](https://box.kancloud.cn/2015-10-30_5632e1c056361.jpg)](http://home.cnblogs.com/u/mchina/) [David_Tang](http://home.cnblogs.com/u/mchina/) [关注 - 1](http://home.cnblogs.com/u/mchina/followees) [粉丝 - 116](http://home.cnblogs.com/u/mchina/followers) [+加关注]() 0 0 (请您对文章做出评价) [« ](http://www.cnblogs.com/mchina/archive/2013/04/10/3012493.html) 上一篇:[PostgreSQL 序列(SEQUENCE)](http://www.cnblogs.com/mchina/archive/2013/04/10/3012493.html "发布于2013-04-10 15:52") [» ](http://www.cnblogs.com/mchina/archive/2013/04/15/3022086.html) 下一篇:[PostgreSQL删除重复数据](http://www.cnblogs.com/mchina/archive/2013/04/15/3022086.html "发布于2013-04-15 14:47") posted @ 2013-04-15 11:56[David_Tang](http://www.cnblogs.com/mchina/) 阅读(92) 评论(0) [编辑](http://www.cnblogs.com/mchina/admin/EditPosts.aspx?postid=3010418)[收藏](#) ![](https://box.kancloud.cn/2015-10-30_5632e1c063548.jpg) Copyright ©2013 David_Tang