PG的时间函数使用整理

PG的时间函数使用整理如下

1.获取系统时间函数
select now(); –2012-05-12 18:51:59.562+08
select current_timestamp; –2012-05-12 18:52:12.062+08
select current_date; –2012-05-12
select current_time; –18:53:23.234+08
2.时间的计算
–使用interval
select now()+interval ‘2 day’; –2012-05-14 20:05:32.796+08 2天后
select now()-interval ‘2 day’; –2012-05-10 20:07:23.265+08 2天前
select now()+interval ‘2 hour’; –2012-05-12 22:06:38.375+08 2小时后

 

select now() + interval '2 years';
select now() + interval '2 year'; 
select now() + interval '2 y';
select now() + interval '2 Y';
select now() + interval '2Y';
 

3.时间的截取
–使用extract extract(interval,timestamp);
select extract(year from now()); –2012
select extract(mon from now()); –5 5月份

4.时间的转换
select timestamp ‘2012-05-12 18:54:54’; –2012-05-12 18:54:54
select date ‘2012-05-12 18:54:54’; –2012-05-12
select time ‘2012-05-12 18:54:54’; –18:54:54
select TIMESTAMP WITH TIME ZONE ‘2012-05-12 18:54:54’
–2012-05-12 18:54:54+08

–与unix时间戳的转换
SELECT TIMESTAMP ‘epoch’ + 1341174767 * INTERVAL ‘1 second’;
–2012-07-01 20:32:47
以上是基本的PG函数使用,可满足一般的开发运维应用

5、格式化函数

时间转字符串

to_char(timestamp,text)
这里写图片描述

字符串转日期

to_date(text,text)
这里写图片描述

字符串转时间

to_timestamp(text,text)
这里写图片描述

Unix时间戳转时间

to_timestamp(unixtime)
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值