Date and time construction functions
Use these functions to build a date, a time, or a timestamp from its parts, such as the year, the month, and the day. To build a TIMESTAMP WITH TIME ZONE value, you can also name the time zone that the parts are in.
make_date function
Combines year, month, and day into one DATE value. You can also pass the three parts as one STRUCT with the fields year, month, and day, or pass a single INTEGER that is the number of days since 1970-01-01. A negative number of days gives a date before 1970-01-01.
If any part is NULL, the result is NULL.
Syntax
make_date(year, month, day)
make_date(date_struct)
make_date(days)
Arguments
The make_date function takes the following arguments:
year-
The year of the result, as a
BIGINTvalue. month-
The month, as a
BIGINTvalue from1to12. day-
The day of the month, as a
BIGINTvalue from1to the number of days in that month. date_struct-
The date parts, as a
STRUCTwith theBIGINTfieldsyear,month, andday. All three fields are required. days-
The number of days since 1970-01-01, as an
INTEGERvalue.
Return type
DATE
Examples
The following SQL query builds a date from a year, month, and day.
SELECT make_date(1992, 9, 20) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query builds a date from a STRUCT of the three parts.
SELECT make_date( {'year': 2024, 'month': 11, 'day': 14} ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the date that is 19,000 days after 1970-01-01.
SELECT make_date(19000) AS result
The query returns the following result.
| result |
|---|
|
Considerations
If the parts do not form a valid date, such as February 30 or a month of 13, the query fails instead of returning NULL.
The one-argument form takes an INTEGER, not a BIGINT. A BIGINT value, such as the result of subtracting one DATE from another, makes the query fail, so cast it first, as in make_date(CAST(.days AS INTEGER))
make_time function
Combines hour, minute, and seconds into one TIME value. seconds can have a fractional part, which is kept to the microsecond.
If any part is NULL, the result is NULL. To build a date and a time together, use the make_timestamp function.
Syntax
make_time(hour, minute, seconds)
Arguments
The make_time function takes the following arguments:
hour-
The hour, as a
BIGINTvalue from0to23. minute-
The minute, as a
BIGINTvalue from0to59. seconds-
The seconds, as a
DOUBLEvalue from0to60. It can have a fractional part.
Return type
TIME
Examples
The following SQL query builds a time with fractional seconds.
SELECT make_time(13, 34, 27.123456) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query builds a time with whole seconds.
SELECT make_time(9, 5, 30) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a NULL part gives a NULL result.
SELECT make_time(9, 5, NULL) IS NULL AS is_null
The query returns the following result.
| is_null |
|---|
|
Considerations
If a part is out of range, such as an hour of 25 or a minute of 61, the query fails instead of returning NULL.
seconds is rounded to the nearest microsecond, and the rounding can carry into the minute. For example, make_time(9, 5, 59.9999999) returns 09:06:00. A seconds value of exactly 60 also carries into the next minute, so make_time(10, 0, 60) returns 10:01:00.
make_timestamp function
Combines year, month, day, hour, minute, and seconds into one TIMESTAMP value. seconds can have a fractional part, which is kept to the microsecond.
With one argument, returns the TIMESTAMP that is microseconds microseconds after 1970-01-01 00:00:00. For a number of milliseconds, use the make_timestamp_ms function, and for nanoseconds, use the make_timestamp_ns function.
If any argument is NULL, the result is NULL. The result has no time zone. To build a TIMESTAMP WITH TIME ZONE value, use the make_timestamptz function.
Syntax
make_timestamp(year, month, day, hour, minute, seconds)
make_timestamp(microseconds)
Arguments
The make_timestamp function takes the following arguments:
year-
The year of the result, as a
BIGINTvalue. month-
The month, as a
BIGINTvalue from1to12. day-
The day of the month, as a
BIGINTvalue. hour-
The hour, as a
BIGINTvalue from0to23. minute-
The minute, as a
BIGINTvalue from0to59. seconds-
The seconds, as a
DOUBLEvalue. It can have a fractional part. microseconds-
The number of microseconds since 1970-01-01 00:00:00, as a
BIGINTvalue.
Return type
TIMESTAMP
Examples
The following SQL query builds a timestamp from its parts.
SELECT make_timestamp( 1992, 9, 20, 13, 34, 27.123456 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query builds a timestamp from a number of microseconds since 1970-01-01 00:00:00.
SELECT make_timestamp(1667810584123456) AS result
The query returns the following result.
| result |
|---|
|
Considerations
If the parts do not form a valid date and time, such as February 30 or a minute of 60, the query fails instead of returning NULL.
make_timestamptz function
Combines year, month, day, hour, minute, and seconds into one TIMESTAMP WITH TIME ZONE value. The parts are read as local time in timezone. Without timezone, they are read in the session time zone, which is UTC. The result is shown in UTC, so a value built in another time zone shows the matching UTC time.
With one argument, returns the TIMESTAMP WITH TIME ZONE value that is microseconds microseconds after 1970-01-01 00:00:00 UTC.
If any argument is NULL, the result is NULL. To build a value with no time zone, use the make_timestamp function.
Syntax
make_timestamptz(year, month, day, hour, minute, seconds
[, timezone ])
make_timestamptz(microseconds)
Arguments
The make_timestamptz function takes the following arguments:
year-
The year of the result, as a
BIGINTvalue. month-
The month, as a
BIGINTvalue from1to12. day-
The day of the month, as a
BIGINTvalue. hour-
The hour, as a
BIGINTvalue from0to23. minute-
The minute, as a
BIGINTvalue from0to59. seconds-
The seconds, as a
DOUBLEvalue. It can have a fractional part. timezone-
The name of the time zone of the parts, as a
VARCHARvalue, such as'America/New_York'. The default is the session time zone, UTC. This argument is optional. microseconds-
The number of microseconds since 1970-01-01 00:00:00 UTC, as a
BIGINTvalue.
Return type
TIMESTAMP WITH TIME ZONE (TIMESTAMPTZ)
Examples
The following SQL query builds a timestamp with time zone from its parts. With no timezone, the parts are read as UTC.
SELECT make_timestamptz( 1992, 9, 20, 13, 34, 27.123456 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query reads the same parts as local time in New York. The result shows the matching time in UTC, four hours later.
SELECT make_timestamptz( 1992, 9, 20, 13, 34, 27.123456, 'America/New_York' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query builds a timestamp with time zone from a number of microseconds since 1970-01-01 00:00:00 UTC.
SELECT make_timestamptz(1667810584123456) AS result
The query returns the following result.
| result |
|---|
|
Considerations
timezone must be a time zone name, such as 'America/New_York', or an abbreviation such as 'EST'. An offset string such as '+05:00' is not a time zone name, and the query fails. To list the names that you can use, query pg_timezone_names().
Parts that are out of range, such as February 30, a month of 13, or a minute of 60, do not make the query fail. Each one carries into the next larger part instead. For example, make_timestamptz(1992, 2, 30, 13, 34, 27.123456) returns 1992-03-01 13:34:27.123456+00, make_timestamptz(1992, 13, 1, 0, 0, 0) returns 1993-01-01 00:00:00+00, and make_timestamptz(1992, 9, 20, 13, 60, 0) returns 1992-09-20 14:00:00+00. This differs from make_timestamp, which fails for a minute of 60.