DATE_FORMAT_pg

date_format_pg(expr, fmt)

Description

The date_format_pg function is used to convert different types of time expressions into string form according to the specified format. This function is compatible with PostgreSQL database date formatting features.

Parameter Description

ParameterTypeDescription
exprdate/timestamp_ltz/stringThe input time expression, which can be in the format of a string, timestamp, datetime, etc.
fmtstringA string describing the format, refer to the PostgreSQL official documentation for specific formats.

Supported Formats

Below are some of the supported formats and their descriptions:

FormatDescription
HHHour (12-hour format, 01-12)
HH12Hour (12-hour format, 01-12)
HH24Hour (24-hour format, 00-23)
MIMinute (00-59)
SSSecond (00-59)
MSMillisecond (000-999)
USMicrosecond (000000-999999)
YYYYYear (4 or more digits)
MMMonth (01-12)
DDDay of the month (01-31)
DDay of the week, Sunday (1) to Saturday (7)

Return Type

  • string: The formatted string.

Usage Example

-- Convert a string type time expression to date format
SELECT date_format_pg('2022-05-01', 'YYYY-MM-DD');

-- Result: '2022-05-01'

-- Convert the current timestamp to a string with hours, minutes, and seconds
SELECT date_format_pg(TIMESTAMP "2022-05-01 12:34:56", 'YYYY-MM-DD HH24:MI:SS');

-- Result: '2022-05-01 12:34:56'

-- Convert the current time to ISO 8601 format
SELECT date_format_pg(TIMESTAMP "2022-05-01 12:34:56", 'YYYY-MM-DD"T"HH24:MI:SS.MS');

-- Result: '2022-05-01T12:34:56.MS'

Notes

  • Please ensure that the format string in the fmt parameter is correct, otherwise the conversion result may not meet expectations.
  • This function may behave differently under different regional settings, for example, the names of months and weekdays may change according to localization settings.
  • For more supported formats and details, please refer to the PostgreSQL official documentation.