PostgreSQL设置时区、时间/日期函数汇总大全

2024-03-01 0 764
目录
  • 前言
  • 查看时区
  • 修改时区
  • 时间/日期操作符和函数
    • 时间/日期操作符
    • 日期/时间函数:
    • extract,date_part函数支持的field
  • 数据类型格式化函数
    • 用于日期/时间格式化的模式:
  • 扩展
    • 总结

      前言

      本文基于 PostgreSQL 12.6 版本,不同版本的函数可能存在差异。

      查看版本psql –version。

      查看时区

      show timezone; –UTC
      select now(); — 2023-07-24 09:22:48.589640 +00:00

      视图pg_timezone_names保存了所有可供选择的时区

      select * from pg_timezone_names;

      查询PRC时区

      select * from pg_timezone_names where name = \’PRC\’;

      PostgreSQL设置时区、时间/日期函数汇总大全

      PRC是指中华人民共和国 PRC(People’s Republic of China)。

      修改时区

      修改时区,设置成东八区 北京时间 UTC+8,默认为session级配置

      set time zone \’PRC\’;
      select now(); — 2023-07-24 17:21:05.086183 +08:00

      修改时区,用户级配置

      alter role rolname set timezone=\’UTC\’; — 修改指定角色时区(rolname为角色名)
      alter role all set timezone=\’UTC\’; — 修改所有角色时区

      修改时区,数据库级配置

      alter database dbname set timezone=\’UTC\’; — dbname为数据库名称

      时间/日期操作符和函数

      时间/日期操作符

      操作符例子返回类型结果+select date ‘2023-07-24’ + integer ‘7’;date2023-07-31+select date ‘2023-07-24’ + interval ‘1 hour’;timestamp2023-07-24 01:00:00.000000+select date ‘2023-07-24’ + time ‘15:16’;timestamp2023-07-24 15:16:00.000000+select interval ‘1 day’ + interval ‘1 hour’;interval0 years 0 mons 1 days 1 hours 0 mins 0.0 secs+select timestamp ‘2023-07-24 15:16’ + interval ‘23 hours’;timestamp2023-07-25 14:16:00.000000+select time ‘01:00’ + interval ‘3 hours’;time04:00:00-select – interval ‘23 hours’;interval0 years 0 mons 0 days -23 hours 0 mins 0.0 secs-select date ‘2023-07-24’ – date ‘2023-07-22’;integer2-select date ‘2023-07-24’ – integer ‘7’;date2023-07-17-select date ‘2023-07-24’ – interval ‘1 hour’;timestamp2023-07-23 23:00:00.000000-select time ‘05:00’ – time ‘03:00’;interval0 years 0 mons 0 days 2 hours 0 mins 0.0 secs-select time ‘05:00’ – interval ‘2 hours’;time03:00:00-select timestamp ‘2023-07-24 23:00’ – interval ‘23 hours’;timestamp2023-07-24 00:00:00.000000-select interval ‘1 day’ – interval ‘1 hour’;interval0 years 0 mons 1 days -1 hours 0 mins 0.0 secs-select timestamp ‘2023-07-24 03:00’ – timestamp ‘2023-07-24 12:00’;interval0 years 0 mons 0 days -9 hours 0 mins 0.0 secs*select interval ‘1 hour’ * double precision ‘3.5’;interval0 years 0 mons 0 days 3 hours 30 mins 0.0 secs/select interval ‘1 hour’ / double precision ‘1.5’;interval0 years 0 mons 0 days 0 hours 40 mins 0.0 secs

      日期/时间函数:

      函数描述例子返回类型结果age(timestamp, timestamp)第1个timestamp 减去 第2个timestampselect age(‘2023-07-24’, ‘1997-10-26’);interval25 years 8 mons 29 days 0 hours 0 mins 0.0 secsage(timestamp)从current_date 减去 timestamp的值select age(timestamp ‘1997-10-26’);interval25 years 8 mons 29 days 0 hours 0 mins 0.0 secscurrent_date今天的日期select current_date;date2023-07-24current_time现在的时间select current_time;time07:53:43.911756 +00:00current_timestamp日期和时间select current_timestamp;timestamp2023-07-24 07:54:19.495372 +00:00date_part(text, timestamp)获取子域(等效于extract)select date_part(‘hour’, timestamp ‘2023-07-24 15:56:34’);double15date_part(text, interval)获取子域(等效于extract)select date_part(‘month’, interval ‘2 years 3 months’);double3date_trunc(text, timestamp)截断成指定的精度select date_trunc(‘hour’, timestamp ‘2023-07-24 15:56:34’);timestamp2023-07-24 15:00:00.000000extract(field from timestamp)获取子域select extract(hour from timestamp ‘2023-07-24 15:56:34’);double15extract(field from interval)获取子域select extract(month from interval ‘2 years 3 months’);double3localtime当前时间select localtime;time08:00:08localtimestamp当前日期和时间select localtimestamp;timestamp2023-07-24 08:05:03.650472now()当前的日期和时间(等效于current_timestamp)select now();timestamp2023-07-24 08:09:30.828408 +00:00timeofday()当前日期和时间select timeofday();textMon Jul 24 08:09:51.870484 2023 UTC

      extract,date_part函数支持的field

      extract,date_part这两个函数可以从日期时间值中提取指定的部分,例如年份、月份、小时等。extract是一个 PostgreSQL 特有的函数,而date_part在标准 SQL 中也有定义,但两者的功能类似。

      域描述例子结果century世纪select extract(century from timestamp ‘2023-07-24 15:56:34’);21day(月份)里的日期域(1-31)select extract(day from timestamp ‘2023-07-24 15:56:34’);24decade年份域除以10select extract(decade from timestamp ‘2023-07-24 15:56:34’);202dow每周的星期号(0-6;星期天是0) (仅用于timestamp)select extract(dow from timestamp ‘2023-07-24 15:56:34’);1doy一年的第几天(1 -365/366) (仅用于 timestamp)select extract(doy from timestamp ‘2023-07-24 15:56:34’);205epochUnix时间戳select extract(epoch from timestamp ‘2023-07-24 15:56:34’);1690214194hour小时域(0-23)select extract(hour from timestamp ‘2023-07-24 15:56:34’);15isodowISO 周几(1-7,其中1代表星期一)select extract(isodow from timestamp ‘2023-07-24 15:56:34’);1isoyearISO 年份select extract(isoyear from timestamp ‘2023-07-24 15:56:34’);2023millennium千年((年份/1000)+1)select extract(millennium from timestamp ‘2023-07-24 15:56:34’);3microseconds微秒select extract(microseconds from TIME ‘15:56:34.5’);34500000millisecond毫秒select extract(millisecon from TIME ‘15:56:34.5’);34500minute分钟(0-59)select extract(minute from timestamp ‘2023-07-24 15:56:34’);56month月份,对于timestamp数值,它是一年里的月份数(1-12);对于interval数值,它是月的数目,然后对12取模(0-11)select extract(month from timestamp ‘2023-07-24 15:56:34’);7quarter季度,该天所在的该年的季度(1-4)(仅用于 timestamp)select extract(quarter from timestamp ‘2023-07-24 15:56:34’);3second秒域,包括小数部分(0-59[1])select extract(second from timestamp ‘2023-07-24 15:56:34’);34week该天在所在的年份里是第几周。select extract(week from timestamp ‘2023-07-24 15:56:34’);30year年份域select extract(year from timestamp ‘2023-07-24 15:56:34’);2023

      数据类型格式化函数

      PostgreSQL格式化函数提供一套有效的工具用于把各种数据类型(日期/时间、integer、floating point和numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成指定的数据类型。

      to_char函数第一个参数是待格式化的值,而第二个是定义输出或输出格式的模板。

      函数描述例子返回类型结果to_char(timestamp, text)把时间戳转换成字串select to_char(current_timestamp, ‘HH12:MI:SS’);text06:03:19to_char(interval, text)把时间间隔转为字串select to_char(interval ‘14h 6m 20s’, ‘HH24:MI:SS’);text14:06:20to_date(text, text)把字串转换成日期select to_date(‘25 Jul 2023’, ‘DD Mon YYYY’);date2023-07-24to_timestamp(text, text)把字串转换成时间戳select to_timestamp(‘25 Jul 2023’, ‘DD Mon YYYY’);timestamp2023-07-24 00:00:00.000000 +00:00to_timestamp(double)把UNIX纪元转换成时间戳select to_timestamp(1690179888);timestamp2023-07-24 06:24:48.000000 +00:00

      用于日期/时间格式化的模式:

      模式描述HH一天的小时数(01-12)HH12一天的小时数(01-12)HH24一天的小时数(00-23)MI分钟(00-59)SS秒(00-59)MS毫秒(000-999)US微秒(000000-999999)AM正午标识(大写)Y,YYY带逗号的年(4和更多位)YYYY年(4和更多位)YYY年的后三位YY年的后两位Y年的最后一位MONTH全长大写月份名(空白填充为9字符)Month全长混合大小写月份名(空白填充为9字符)month全长小写月份名(空白填充为9字符)MON大写缩写月份名(3字符)Mon缩写混合大小写月份名(3字符)mon小写缩写月份名(3字符)MM月份号(01-12)DAY全长大写日期名(空白填充为9字符)Day全长混合大小写日期名(空白填充为9字符)day全长小写日期名(空白填充为9字符)DY缩写大写日期名(3字符)Dy缩写混合大小写日期名(3字符)dy缩写小写日期名(3字符)DDD一年里的日子(001-366)DD一个月里的日子(01-31)D一周里的日子(1-7;周日是1)W一个月里的周数(1-5)(第一周从该月第一天开始)WW一年里的周数(1-53)(第一周从该年的第一天开始)

      示例:

      — 查询今天是今年的第几天
      select to_char(now(), \’DDD\’); — 205

      扩展

      查询某个日期是否在某段日期范围,可以使用>和<判断;如果使用了between,则前一个日期必须小于后一个日期。

      示例:

      — between 错误用法
      select date \’2023-07-24\’ between date(\’2023-07-25\’) – 1 and date(\’2023-07-25\’) – 7; — false
      — between 正确用法
      select date \’2023-07-24\’ between date(\’2023-07-25\’) – 7 and date(\’2023-07-25\’) – 1; — true

      总结

      到此这篇关于PostgreSQL设置时区、时间/日期函数汇总的文章就介绍到这了,更多相关PostgreSQL设置时区时间内容请搜索悠久资源以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源!

      收藏 (0) 打赏

      感谢您的支持,我会继续努力的!

      打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
      点赞 (0)

      悠久资源 PostgreSQL PostgreSQL设置时区、时间/日期函数汇总大全 https://www.u-9.cn/database/postgresql/182770.html

      常见问题

      相关文章

      发表评论
      暂无评论
      官方客服团队

      为您解决烦忧 - 24小时在线 专业服务