postgresql-common date functions

Introduction

PostgreSQL provides the following arithmetic operators for date and time operations.
Insert image description here
Insert image description here
Get the current system time

select current_date,current_time,current_timestamp ;

Insert image description here

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

Insert image description here

Calculate time interval

The age(timestamp, timestamp) function is used to calculate the interval between two time points, and the age(timestamp) function is used to
calculate the interval between 12 a.m. on the current date and this time point.

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

Insert image description here

Get information from time

The date_part(text, timestamp) and extract(field FROM timestamp) functions are used to obtain a certain part of the date and time
, such as year, month, hour, etc.; the date_part(text, interval) and extract(field FROM interval) functions
are used to obtain the time. part of the interval

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

Insert image description here

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

Insert image description here

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');

Insert image description here

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

Insert image description here
The extract function actually calls the date_part function, but the parameters are different. The information supported by these two functions includes:

  • century, century;
  • day, for timestamp, returns the day of the month; for interval, returns the number of days;
  • decade, the year divided by 10;
  • dow, Sunday (0) to Saturday (6);
  • doy, day of the year, (1 - 365/366);
  • epoch, for timestamp WITH time zone, return the number of seconds from 1970-01-01 00:00:00 UTC to this time;
    for date and timestamp, return the number of seconds from 1970-01-01 00:00:00 local time to this time Number of seconds; for
    interval, returns the interval in seconds;
  • hour, hour (1 - 23);
  • isodow, Monday (1) to Sunday (7) in the ISO 8601 standard
  • isoyear, the year of the date defined by the ISO 8601 standard. Each year starts on the Monday that includes January 4th, and
    January 1st, 2017 belongs to 2016;
  • microseconds, microseconds, numbers including seconds and fractional seconds multiplied by 1,000,000;
  • millennium, millennium;
  • milliseconds, milliseconds, numbers including seconds and fractional seconds multiplied by 1000;
  • minute, minute, (0 - 59);
  • month, month;
  • quarter, quarter, (1 - 4);
  • second, seconds, including fractional seconds;
  • timezone, UTC time zone, unit is seconds;
  • timezone_hour, hour part in UTC time zone;
  • timezone_minute, minute part in UTC time zone;
  • week, the day of the week in the ISO 8601 standard, starting from the week on the first Thursday of each year;
  • year, year.
-- 计算当前日期从1970年到现在的秒数
select extract('epoch' from current_date);

Insert image description here

Cutoff date/time

The date_trunc(field, source [, time_zone]) function is used to truncate timestamp, timestamp WITH time zone,
date, time or interval data to the specified precision.
The date_trunc function supports the following truncation precision:

  • 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');

Insert image description here

Creation date/time

make_date(year int, month int, day int)The function is used to create a date:
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)the function creates a time interval by specifying the year, month, day and other information.
make_time(hour int, min int, sec double precision)The function creates a
time by specifying hours, minutes, and seconds.
make_timestamp(year int, month int, day int, hour int, min int, sec double precision) The function creates a timestamp by specifying the year, month, day, hour, minute, and second. The
make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])function creates a timestamp with a time zone by specifying the year, month, day, hour, minute, and second. If no
time zone is specified, the current time zone
to_timestamp(double precision)function is used to convert Unix timestamps (number of seconds since 1970-01-01 00:00:00+00
) to PostgreSQL timestamp data.

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
;

Insert image description here

Get system time

PostgreSQL provides a large number of functions for obtaining the current date and time of the system, such as current_date, current_time,
current_timestamp, clock_timestamp(), localtimestamp, now(), statement_timestamp(), etc. It also supports function reference
such as pg_sleep() that delays statement execution.
article

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

Insert image description here

time zone conversion

The AT TIME ZONE operator is used to convert timestamp without time zone, timestamp WITH time zone and
time WITH time zone into the time in the specified time zone.
The timezone(zone, timestamp) function is equivalent to timestamp AT TIME ZONE zone in the SQL standard.
Official website introduction

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';

Insert image description here

Guess you like

Origin blog.csdn.net/Java_Fly1/article/details/132658903