PG的时间函数使用整理如下
1.获取系统时间函数
1select now(); --2012-05-12 18:51:59.562+08 2select current_timestamp; --2012-05-12 18:52:12.062+08 3select current_date; --2012-05-12 4select current_time; --18:53:23.234+08
2.时间的计算
--使用interval
1select now()+interval '2 day'; --2012-05-14 20:05:32.796+08 2天后 2select now()-interval '2 day'; --2012-05-10 20:07:23.265+08 2天前 3select now()+interval '2 hour'; --2012-05-12 22:06:38.375+08 2小时后 4.... 5 6interval可以不写,其值可以是 7Abbreviation Meaning 8Y Years 9M Months (in the date part) 10W Weeks 11D Days 12H Hours 13M Minutes (in the time part)
3.时间的截取
--使用extract extract(interval,timestamp);
1select extract(year from now()); --2012 2select extract(mon from now()); --5 5月份 3... 4 5interval值参考上面
4.时间的转换
1select timestamp '2012-05-12 18:54:54'; --2012-05-12 18:54:54 2select date '2012-05-12 18:54:54'; --2012-05-12 3select time '2012-05-12 18:54:54'; --18:54:54 4select TIMESTAMP WITH TIME ZONE '2012-05-12 18:54:54' 5--2012-05-12 18:54:54+08 6 7--与unix时间戳的转换 8SELECT TIMESTAMP 'epoch' + 1341174767 * INTERVAL '1 second'; 9--2012-07-01 20:32:47
以上是基本的PG函数使用,可满足一般的开发运维应用