PostgreSQL日期和时间操作大全

Posted yqj234

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSQL日期和时间操作大全相关的知识,希望对你有一定的参考价值。

1. 官网帮忙文档

        日期时间函数网址

1.2 函数使用方法

PatternDescriptionRemark
HHhour of day (01–12)
HH12hour of day (01–12)
HH24hour of day (00–23)
MIminute (00–59)
SSsecond (00–59)
MSmillisecond (000–999)
USmicrosecond (000000–999999)
FF1tenth of second (0–9)
FF2hundredth of second (00–99)
FF3millisecond (000–999)
FF4tenth of a millisecond (0000–9999)
FF5hundredth of a millisecond (00000–99999)
FF6microsecond (000000–999999)
SSSSSSSSSseconds past midnight (0–86399)
AMamPM or pmmeridiem indicator (without periods)
A.M.a.m.P.M. or p.m.meridiem indicator (with periods)
Y,YYYyear (4 or more digits) with comma
YYYYyear (4 or more digits)
YYYlast 3 digits of year
YYlast 2 digits of year
Ylast digit of year
IYYYISO 8601 week-numbering year (4 or more digits)
IYYlast 3 digits of ISO 8601 week-numbering year
IYlast 2 digits of ISO 8601 week-numbering year
Ilast digit of ISO 8601 week-numbering year
BCbcAD or adera indicator (without periods)
B.C.b.c.A.D. or a.d.era indicator (with periods)
MONTHfull upper case month name (blank-padded to 9 chars)
Monthfull capitalized month name (blank-padded to 9 chars)
monthfull lower case month name (blank-padded to 9 chars)
MONabbreviated upper case month name (3 chars in English, localized lengths vary)
Monabbreviated capitalized month name (3 chars in English, localized lengths vary)
monabbreviated lower case month name (3 chars in English, localized lengths vary)
MMmonth number (01–12)
DAYfull upper case day name (blank-padded to 9 chars)
Dayfull capitalized day name (blank-padded to 9 chars)
dayfull lower case day name (blank-padded to 9 chars)
DYabbreviated upper case day name (3 chars in English, localized lengths vary)星期几的英文,前3个字母,3个字母都大写
Dyabbreviated capitalized day name (3 chars in English, localized lengths vary)星期几的英文,前3个字母,首字线大写,后2个字母小写
dyabbreviated lower case day name (3 chars in English, localized lengths vary)星期几的英文,前3个字母,3个字母都为小写
DDDday of year (001–366)
IDDDday of ISO 8601 week-numbering year (001–371; day 1 of the year is Monday of the first ISO week)
DDday of month (01–31)
Dday of the week, Sunday (1) to Saturday (7)
IDISO 8601 day of the week, Monday (1) to Sunday (7)
Wweek of month (1–5) (the first week starts on the first day of the month)月里的周别,1号为周别开始的日期,7天为一个周别,来计算,例如:2023年1月份的第1周的日期为(1月1号至1月7号)第2周的日期为(1月8号至1月14号)
WWweek number of year (1–53) (the first week starts on the first day of the year)年里的周别:1月1号开始计算,7天为周别,依次来计算,例如2022年第1周为(1月1号至1月7号),第2周为(1月8号至1月5号)
IWweek number of ISO 8601 week-numbering year (01–53; the first Thursday of the year is in week 1)ISO 8601周编号年份的周编号(01–53;一年的第一个星期四在第1周):看一年里第一个星期四落在的周,为本年的第一周
CCcentury (2 digits) (the twenty-first century starts on 2001-01-01)
JJulian Date (integer days since November 24, 4714 BC at local midnight; see Section B.7)
Qquarter
RMmonth in upper case Roman numerals (I–XII; I=January)
rmmonth in lower case Roman numerals (i–xii; i=January)
TZupper case time-zone abbreviation (only supported in to_char)
tzlower case time-zone abbreviation (only supported in to_char)
TZHtime-zone hours
TZMtime-zone minutes
OFtime-zone offset from UTC (only supported in to_char)


2.操作实例 

select now(); --当前时间 2021-04-21 11:36:56

SELECT EXTRACT (year FROM now()); --年 2021
SELECT EXTRACT (quarter FROM now()); --季 02
SELECT EXTRACT (month FROM now()); --月 04
SELECT EXTRACT (day FROM now()); --月中天 21
SELECT EXTRACT (week FROM now()); --周 16
SELECT EXTRACT (isodow FROM now()); --周中天 3
SELECT EXTRACT (hour FROM now()); --時 11
SELECT EXTRACT (minute FROM now()); --分 36
SELECT EXTRACT (second FROM now()); --秒 56.909161

2.1 周别和星期操作大全

2.1.1 求周别的函数

select  to_char(to_date('2023-01-02','YYYY-MM-DD'),'W');
select  to_char(to_date('2023-01-02','YYYY-MM-DD'),'WW');
select  to_char(to_date('2023-01-02','YYYY-MM-DD'),'IW');

以上是关于PostgreSQL日期和时间操作大全的主要内容,如果未能解决你的问题,请参考以下文章

PostgreSQL 时间/日期函数和操作符

PostgreSQL 时间/日期函数和操作符

Sql日期时间格式转换大全

SQL日期格式化应用大全

如何从 SQLite3 和 PostgreSQL 中的时间戳中选择日期

PostgreSQL之SQL操作符介绍及实践