记住年月日时分秒的模式字符串:
YYYY-MM-DD HH24:MI:SS
-
日期 --> 字符串
to_char(date, formatString)
select to_char(now(), 'YYYY-MM-DD HH24, MI:SS')
-
字符串 --> 日期
to_date('dateString', 'formatString')
select to_date('2021-12-18 22:24:15', 'YYYY-MM-DD HH24:MI:SS')
官网链接
https://www.postgresql.org/docs/12/functions-formatting.html
完整的表格:
Pattern
|
Description
|
HH
|
hour of day (01-12)
|
HH12
|
hour of day (01-12)
|
HH24
|
hour of day (00-23)
|
MI
|
minute (00-59)
|
SS
|
second (00-59)
|
MS
|
millisecond (000-999)
|
US
|
microsecond (000000-999999)
|
SSSS
|
seconds past midnight (0-86399)
|
AM, am, PM or pm
|
meridiem indicator (without periods)
|
A.M., a.m., P.M. or p.m.
|
meridiem indicator (with periods)
|
Y,YYY
|
year (4 or more digits) with comma
|
YYYY
|
year (4 or more digits)
|
YYY
|
last 3 digits of year
|
YY
|
last 2 digits of year
|
Y
|
last digit of year
|
IYYY
|
ISO 8601 week-numbering year (4 or more digits)
|
IYY
|
last 3 digits of ISO 8601 week-numbering year
|
IY
|
last 2 digits of ISO 8601 week-numbering year
|
I
|
last digit of ISO 8601 week-numbering year
|
BC, bc, AD or ad
|
era indicator (without periods)
|
B.C., b.c., A.D. or a.d.
|
era indicator (with periods)
|
MONTH
|
full upper case month name (blank-padded to 9 chars)
|
Month
|
full capitalized month name (blank-padded to 9 chars)
|
month
|
full lower case month name (blank-padded to 9 chars)
|
MON
|
abbreviated upper case month name (3 chars in English, localized lengths vary)
|
Mon
|
abbreviated capitalized month name (3 chars in English, localized lengths vary)
|
mon
|
abbreviated lower case month name (3 chars in English, localized lengths vary)
|
MM
|
month number (01-12)
|
DAY
|
full upper case day name (blank-padded to 9 chars)
|
Day
|
full capitalized day name (blank-padded to 9 chars)
|
day
|
full lower case day name (blank-padded to 9 chars)
|
DY
|
abbreviated upper case day name (3 chars in English, localized lengths vary)
|
Dy
|
abbreviated capitalized day name (3 chars in English, localized lengths vary)
|
dy
|
abbreviated lower case day name (3 chars in English, localized lengths vary)
|
DDD
|
day of year (001-366)
|
IDDD
|
day of ISO 8601 week-numbering year (001-371; day 1 of the year is Monday of the first ISO week)
|
DD
|
day of month (01-31)
|
D
|
day of the week, Sunday (1) to Saturday (7)
|
ID
|
ISO 8601 day of the week, Monday (1) to Sunday (7)
|
W
|
week of month (1-5) (the first week starts on the first day of the month)
|
WW
|
week number of year (1-53) (the first week starts on the first day of the year)
|
IW
|
week number of ISO 8601 week-numbering year (01-53; the first Thursday of the year is in week 1)
|
CC
|
century (2 digits) (the twenty-first century starts on 2001-01-01)
|
J
|
Julian Date (integer days since November 24, 4714 BC at local midnight; see Section B.7)
|
Q
|
quarter
|
RM
|
month in upper case Roman numerals (I-XII; I=January)
|
rm
|
month in lower case Roman numerals (i-xii; i=January)
|
TZ
|
upper case time-zone abbreviation (only supported in to_char)
|
tz
|
lower case time-zone abbreviation (only supported in to_char)
|
TZH
|
time-zone hours
|
TZM
|
time-zone minutes
|
OF
|
time-zone offset from UTC (only supported in to_char)
|
函数记住年月日时分秒的模式字符串:YYYY-MM-DD HH24:MI:SS日期 --> 字符串 to_char(date, formatString)select to_char(now(), 'YYYY-MM-DD HH24, MI:SS')字符串 --> 日期 to_date('dateString', 'formatString')select to_date('2021-12-18 22:24:15', 'YYYY-MM-DD HH24:MI:SS')ref官网
这里是对
postgresql
使用时踩的坑的记录
上图来一波官方文档
PostgreSQL
文档地址 https://www.
postgresql
.org/docs/8.1/functions-formatting.html
1.to_
date
(text,text), 在
PostgreSQL
里 也是将
字符串
转换
成
日期
, 但是 仅仅是年月日部分,就算我们在方法里指定了格式亦是如此,比如:
selec...
to_
char
(time,'
YYYY
-
MM
-
DD
h
h2
4:
mi
:
ss
') as time1,
to_
char
(time,'
YYYY
-
MM
-
DD
') as time2,
to_
char
(time,'
YYYY
-
MM
-
DD
hh:
mi
:
ss
') as time3
timestamp去掉时区 即到时分秒select current_timestamp(0)::timestamp witho...
1、pgsql中
日期
转
字符串
的方法
首先以不带时区、不带精确度、只到秒的
日期
时间:
select now()::timestamp(0) without time zone
查询结果为:
若想把返回结果
转
为
字符串
型,可用to_
char
(arg1, arg2)函数,第一个参数是
日期
/时间本身,第二个参数为一个
字符串
,表示想要
转换
成的格式,
格式化为年-月-日 时间:分:秒的格式:
select to_
char
(now()::timestam
postgre SQL 中的時間類型
有4種:timestamp (時間類型 格式 : ‘
YYYY
-
MM
-
DD
H
H2
4:
MI
:
SS
’)
date
(
日期
格式: ‘
YYYY
-
MM
-
DD
’)
time (時間 格式: ‘H
H2
4:
MI
:
SS
’)
interval (間隔 格式: day hour hours second )
時間進行計算時...
PostgreSQL
-PostGIS-TimescaleDB
PostgreSQL
+ PostGIS + TimescaleDB即用型Docker映像 :elephant: :globe_showing_Americas: :
char
t_increasing:
Docker映像具有:
当前组件版本:
PostgreSQL
: 12.5 ()
PostGIS: 3.1.1 ()
TimescaleDB: 2.0.1 ()
如何建造:
$ docker build -t binakot/
postgresql
-postgis-timescaledb .
如何运行:
$ docker run -d --name postgres -e POSTGRES_PA
SS
WORD=postgres binakot/
postgresql
-postgis-timescaledb
您也可以使用构建的docker映像和pgAd
mi
n4运行应用程序堆栈:docker docke
在
PostgreSQL
中,获取
yyyy
-
mm
-
dd
HH:
mm
:
ss
格式的
日期
时间的前一天,可以使用 to_timestamp() 函数将
字符串
转换
为 timestamp 类型,然后使用 interval 表达式进行计算。具体方法如下:
```sql
SELECT to_timestamp('2022-03-01 10:20:30', '
YYYY
-
MM
-
DD
H
H2
4:
MI
:
SS
') - interval '1 day';
其中,to_timestamp() 函数将 '2022-03-01 10:20:30'
字符串
转换
为 timestamp 类型的
日期
时间值。然后使用 interval '1 day' 表示一个一天的时间间隔,并通过减法运算符将其从 timestamp 值中减去,即可得到前一天的
日期
时间值。
这个查询会返回 2022-02-28 10:20:30,即指定
日期
时间 '2022-03-01 10:20:30' 的前一天的
日期
时间值。
SpringBoot Swagger 禁用/启用 配置