PostgreSql date time output style and order

I. Overview

  The output format and order of date and time in PostgreSQL are controlled by the datestyle parameter, which can be set independently or together. The default is ISO,MDY.

2. Output format

  The output format of the time/date type in PostgreSQL can be set to one of four styles: ISO 8601, SQL (Ingres), traditional POSTGRES (Unix date format), or German. The default is the ISO format (the ISO standard requires the use of the ISO 8601 format).

--ISO 风格
postgres=# set datestyle = 'ISO';
SET
postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 2023-02-27 16:21:40.605044+08
(1 row)

--SQL 风格
postgres=# set datestyle = 'SQL';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 02/27/2023 16:22:00.884861 CST
(1 row)

--POSTGRES 风格
postgres=# set datestyle = 'POSTGRES';
SET
postgres=# select current_timestamp;
          current_timestamp
-------------------------------------
 Mon Feb 27 16:22:14.589116 2023 CST
(1 row)

--German 风格
postgres=# set datestyle = 'German';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 27.02.2023 16:22:23.080493 CST
(1 row)

3. Input and output sequence

  The time input and output sequence in PostgreSQL can be set to one of three sequences: DMY, MDY or YMD, and the default is MDY sequence.

--风格为 ISO 时的顺序
postgres=# show datestyle;
 DateStyle
-----------
 ISO, MDY
(1 row)

postgres=# select current_timestamp;
      current_timestamp
------------------------------
 2023-03-06 09:40:51.05446+08
(1 row)

postgres=# set datestyle = 'DMY';
SET
postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 2023-03-06 09:41:24.996177+08
(1 row)

postgres=# set datestyle = 'YMD';
SET
postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 2023-03-06 09:41:35.327119+08
(1 row)

--风格为 SQL 时的顺序
postgres=# set datestyle = 'SQL';
SET
postgres=# show datestyle;
 DateStyle
-----------
 SQL, MDY
(1 row)

postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 03/06/2023 09:45:41.371565 CST
(1 row)

postgres=# set datestyle = 'DMY';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 06/03/2023 09:45:48.797223 CST
(1 row)

postgres=# set datestyle = 'YMD';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 03/06/2023 09:46:04.377456 CST
(1 row)

--风格为 POSTGRES 时的顺序
postgres=# set datestyle = 'POSTGRES';
SET
postgres=# show datestyle;
   DateStyle
---------------
 Postgres, MDY
(1 row)

postgres=# select current_timestamp;
          current_timestamp
-------------------------------------
 Mon Mar 06 09:48:28.792725 2023 CST
(1 row)

postgres=# set datestyle = 'DMY';
SET
postgres=# select current_timestamp;
         current_timestamp
------------------------------------
 Mon 06 Mar 09:48:43.27396 2023 CST
(1 row)

postgres=# set datestyle = 'YMD';
SET
postgres=# select current_timestamp;
          current_timestamp
-------------------------------------
 Mon Mar 06 09:49:10.084696 2023 CST
(1 row)

--风格为 German 时的顺序
postgres=# set datestyle = 'German';
SET
postgres=# show datestyle;
  DateStyle
-------------
 German, DMY
(1 row)

postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 06.03.2023 09:50:41.023778 CST
(1 row)

postgres=# set datestyle = 'MDY';
SET
postgres=# select current_timestamp;
       current_timestamp
--------------------------------
 06.03.2023 09:50:55.125557 CST
(1 row)

postgres=# set datestyle = 'YMD';
SET
postgres=# select current_timestamp;
       current_timestamp
-------------------------------
 06.03.2023 09:51:16.61567 CST
(1 row)

Four. Summary

  • When the style is ISO and German, the time display result is not affected by sorting.
  • When the style is SQL and POSTGRES, the month and day in the time display result are affected by sorting.

Guess you like

Origin blog.csdn.net/songyundong1993/article/details/132160362