postgresql-常用日期函数

简介

PostgreSQL 提供了以下日期和时间运算的算术运算符。
在这里插入图片描述
在这里插入图片描述
获取当前系统时间

select current_date,current_time,current_timestamp ;

在这里插入图片描述

-- 当前系统时间一周后的日期
select current_date + interval '7 day',current_time,current_timestamp ;

在这里插入图片描述

计算时间间隔

age(timestamp, timestamp)函数用于计算两个时间点之间的间隔,age(timestamp)函数用于
计算当前日期的凌晨 12 点到该时间点之间的间隔

select age(now(),date '1988-11-29') as ageResult;

在这里插入图片描述

获取时间中的信息

date_part(text, timestamp)和 extract(field FROM timestamp)函数用于获取日期时间中的某
一部分,例如年份、月份、小时等;date_part(text, interval)和 extract(field FROM interval)函数
用于获取时间间隔中的某一部分

-- 获取当前日期所属年份
select extract ('year' from now()) as t;

在这里插入图片描述

-- 判断当前日期是星期几
select extract ('dow' from now()) as t;

在这里插入图片描述

select date_part('year', timestamp '2020-03-03 20:38:40'), extract(year FROM
timestamp '2020-03-03 20:38:40'),
 date_part('month', interval '1 years 5 months'), extract(month FROM
interval '1 years 5 months');

在这里插入图片描述

select
	date_part('year',
	now()) as "当前年度",
	date_part('month',now()) as "当前月份",
	date_part('day',now()) as "当前日子",
	date_part('dow',now()) as "星期几"
	;

在这里插入图片描述
extract 函数实际上也是调用了 date_part 函数,只是参数方式不同。这两个函数支持获取的信息包括

  • century,世纪;
  • day,对于 timestamp,返回月份中的第几天;对于 interval,返回天数;
  • decade,年份除以 10;
  • dow,星期天(0)到星期六(6);
  • doy,一年中的第几天,(1 - 365/366);
  • epoch,对于 timestamp WITH time zone,返回 1970-01-01 00:00:00 UTC 到该时间的秒数;
    对于 date 和 timestamp,返回本地时间的 1970-01-01 00:00:00 到该时间的秒数;对于
    interval,返回以秒数表示的该时间间隔;
  • hour,小时(1 - 23);
  • isodow,ISO 8601 标准中的星期一(1)到星期天(7)
  • isoyear,ISO 8601 标准定义的日期所在的年份。每年从包含 1 月 4 日的星期一开始,2017
    年 01 月 01 日属于 2016 年;
  • microseconds,微秒,包含秒和小数秒在内的数字乘以 1000000;
  • millennium,千年;
  • milliseconds,毫秒,包含秒和小数秒在内的数字乘以 1000;
  • minute,分钟,(0 - 59);
  • month,月份;
  • quarter,季度,(1 - 4);
  • second,秒数,包含小数秒;
  • timezone,UTC 时区,单位为秒;
  • timezone_hour,UTC 时区中的小时部分;
  • timezone_minute,UTC 时区中的分钟部分;
  • week,ISO 8601 标准中的星期几,每年从第一个星期四所在的一周开始;
  • year,年份。
-- 计算当前日期从1970年到现在的秒数
select extract('epoch' from current_date);

在这里插入图片描述

截断日期/时间

date_trunc(field, source [, time_zone ])函数用于将 timestamp、timestamp WITH time zone、
date、time 或者 interval 数据截断到指定的精度
date_trunc 函数支持以下截断精度:

  • microseconds
  • milliseconds
  • second
  • minute
  • hour
  • day
  • week
  • month
  • quarter
  • year
  • decade
  • century
  • millennium
select date_trunc('year', timestamp '2020-03-03 20:38:40'),
 date_trunc('day', timestamptz '2020-03-03 20:38:40+00',
'asia/shanghai'),
 date_trunc('hour', interval '2 days 3 hours 40 minutes');

在这里插入图片描述

创建日期/时间

make_date(year int, month int, day int)函数用于创建一个日期:
make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0)函数通过指定年、月、日等信息创建一个时间间隔。
make_time(hour int, min int, sec double precision)函数通过指定小时、分钟和秒数创建一个
时间。
make_timestamp(year int, month int, day int, hour int, min int, sec double precision) 函数通过指定年、月、日、时、分、秒创建一个时间戳
make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])函数通过指定年、月、日、时、分、秒创建一个带时区的时间戳。如果没有指
定时区,使用当前时区
to_timestamp(double precision)函数将 Unix 时间戳(自从 1970-01-01 00:00:00+00 以来的秒
数)转换为 PostgreSQL 时间戳数据。

select make_date(2020, 03, 15) as t1,
make_interval(days => 1, hours => 5) as t2,
make_time(1, 2, 30.5) as t3,
make_timestamp(2020, 3, 15, 8, 20, 23.5) as t4,
make_timestamptz(2020, 3, 15, 8, 20, 23.5) as t5,
to_timestamp(1583152349) as t6
;

在这里插入图片描述

获取系统时间

PostgreSQL 提供了大量用于获取系统当前日期和时间的函数,例如 current_date、current_time、
current_timestamp、clock_timestamp()、localtimestamp、now()、statement_timestamp()等;同时还
支持延迟语句执行的 pg_sleep()等函数
参考文章

-- 当前日期
select current_date as t1,
current_time as t2,
localtime as t3, 
current_timestamp as t4,
localtimestamp as t5,
now() as t6
;

在这里插入图片描述

时区转换

AT TIME ZONE 运算符用于将 timestamp without time zone、timestamp WITH time zone 以及
time WITH time zone 转换为指定时区中的时间
timezone(zone, timestamp)函数等价于 SQL 标准中的 timestamp AT TIME ZONE zone。
官网介绍

select timestamp '2020-03-03 20:38:40' at time zone 'asia/shanghai',
 timestamp with time zone '2020-03-03 20:38:40-05:00' at time zone
'asia/shanghai',
 time with time zone '20:38:40-05:00' at time zone 'asia/shanghai';

在这里插入图片描述

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值