postgresql常用日期函數使用整理
簡介
PostgreSQL 提供了以下日期和時間運算的算術運算符。
獲取當前系統(tǒng)時間
select current_date,current_time,current_timestamp ;
-- 當前系統(tǒng)時間一周后的日期 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 時區(qū),單位為秒;
- timezone_hour,UTC 時區(qū)中的小時部分;
- timezone_minute,UTC 時區(qū)中的分鐘部分;
- 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');
創(chuàng)建日期/時間
make_date(year int, month int, day int)
函數用于創(chuàng)建一個日期: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)
函數通過指定年、月、日等信息創(chuàng)建一個時間間隔。make_time(hour int, min int, sec double precision)
函數通過指定小時、分鐘和秒數創(chuàng)建一個
時間。
make_timestamp(year int, month int, day int, hour int, min int, sec double precision)
函數通過指定年、月、日、時、分、秒創(chuàng)建一個時間戳make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])
函數通過指定年、月、日、時、分、秒創(chuàng)建一個帶時區(qū)的時間戳。如果沒有指
定時區(qū),使用當前時區(qū)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 ;
獲取系統(tǒng)時間
PostgreSQL 提供了大量用于獲取系統(tǒng)當前日期和時間的函數,例如 current_date、current_time、
current_timestamp、clock_timestamp()、localtimestamp、now()、statement_timestamp()等;同時還
支持延遲語句執(zhí)行的 pg_sleep()等函數
-- 當前日期 select current_date as t1, current_time as t2, localtime as t3, current_timestamp as t4, localtimestamp as t5, now() as t6 ;
時區(qū)轉換
AT TIME ZONE 運算符用于將 timestamp without time zone、timestamp WITH time zone 以及
time WITH time zone 轉換為指定時區(qū)中的時間
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';
總結
到此這篇關于postgresql常用日期函數使用整理的文章就介紹到這了,更多相關postgresql常用日期函數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!