RefLib

Reference library for AI

Back

postgresql_16:3.5.5.1 Date/Time Input

PostgreSQL 16 > The SQL Language > Data Types > Date/Time Types > Date/Time Input

Date and time input is accepted in almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES, and others. For some formats, ordering of day, month, and year in date input is ambiguous and there is support for specifying the expected ordering of these fields. Set the (see guc-datestyle) parameter to MDY to select month-day-year interpretation, DMY to select day-month-year interpretation, or YMD to select year-month-day interpretation.

PostgreSQL is more flexible in handling date/time input than the SQL standard requires. See (see postgresql_16:9.2) for the exact parsing rules of date/time input and for the recognized text fields including months, days of the week, and time zones.

Remember that any date or time literal input needs to be enclosed in single quotes, like text strings. Refer to (see postgresql_16:3.1.1.2) for more information. SQL requires the following syntax *type* [ (*p*) ] '*value*' where *p* is an optional precision specification giving the number of fractional digits in the seconds field. Precision can be specified for time, timestamp, and interval types, and can range from 0 to 6. If no precision is specified in a constant specification, it defaults to the precision of the literal value (but not more than 6 digits).

Dates

(see postgresql_16:3.5.5.1) shows some possible inputs for the date type.

| Example | Description | | --- | --- | | 1999-01-08 | ISO 8601; January 8 in any mode (recommended format) | | January 8, 1999 | unambiguous in any datestyle input mode | | 1/8/1999 | January 8 in MDY mode; August 1 in DMY mode | | 1/18/1999 | January 18 in MDY mode; rejected in other modes | | 01/02/03 | January 2, 2003 in MDY mode; February 1, 2003 in DMY mode; February 3, 2001 in YMD mode | | 1999-Jan-08 | January 8 in any mode | | Jan-08-1999 | January 8 in any mode | | 08-Jan-1999 | January 8 in any mode | | 99-Jan-08 | January 8 in YMD mode, else error | | 08-Jan-99 | January 8, except error in YMD mode | | Jan-08-99 | January 8, except error in YMD mode | | 19990108 | ISO 8601; January 8, 1999 in any mode | | 990108 | ISO 8601; January 8, 1999 in any mode | | 1999.008 | year and day of year | | J2451187 | Julian date | | January 8, 99 BC | year 99 BC |

Times

The time-of-day types are time [ (*p*) ] without time zone and time [ (*p*) ] with time zone. time alone is equivalent to time without time zone.

Valid input for these types consists of a time of day followed by an optional time zone. (See (see postgresql_16:3.5.5.1) and (see postgresql_16:3.5.5.1).) If a time zone is specified in the input for time without time zone, it is silently ignored. You can also specify a date but it will be ignored, except when you use a time zone name that involves a daylight-savings rule, such as America/New_York. In this case specifying the date is required in order to determine whether standard or daylight-savings time applies. The appropriate time zone offset is recorded in the time with time zone value and is output as stored; it is not adjusted to the active time zone.

| Example | Description | | --- | --- | | 04:05:06.789 | ISO 8601 | | 04:05:06 | ISO 8601 | | 04:05 | ISO 8601 | | 040506 | ISO 8601 | | 04:05 AM | same as 04:05; AM does not affect value | | 04:05 PM | same as 16:05; input hour must be <= 12 | | 04:05:06.789-8 | ISO 8601, with time zone as UTC offset | | 04:05:06-08:00 | ISO 8601, with time zone as UTC offset | | 04:05-08:00 | ISO 8601, with time zone as UTC offset | | 040506-08 | ISO 8601, with time zone as UTC offset | | 040506+0730 | ISO 8601, with fractional-hour time zone as UTC offset | | 040506+07:30:00 | UTC offset specified to seconds (not allowed in ISO 8601) | | 04:05:06 PST | time zone specified by abbreviation | | 2003-04-12 04:05:06 America/New_York | time zone specified by full name |

| Example | Description | | --- | --- | | PST | Abbreviation (for Pacific Standard Time) | | America/New_York | Full time zone name | | PST8PDT | POSIX-style time zone specification | | -8:00:00 | UTC offset for PST | | -8:00 | UTC offset for PST (ISO 8601 extended format) | | -800 | UTC offset for PST (ISO 8601 basic format) | | -8 | UTC offset for PST (ISO 8601 basic format) | | zulu | Military abbreviation for UTC | | z | Short form of zulu (also in ISO 8601) |

Refer to (see postgresql_16:3.5.5.3) for more information on how to specify time zones.

Time Stamps

Valid input for the time stamp types consists of the concatenation of a date and a time, followed by an optional time zone, followed by an optional AD or BC. (Alternatively, AD/BC can appear before the time zone, but this is not the preferred ordering.) Thus: 1999-01-08 04:05:06 and: 1999-01-08 04:05:06 -8:00 are valid values, which follow the ISO 8601 standard. In addition, the common format: January 8 04:05:06 1999 PST is supported.

The SQL standard differentiates timestamp without time zone and timestamp with time zone literals by the presence of a “+” or “-” symbol and time zone offset after the time. Hence, according to the standard, TIMESTAMP '2004-10-19 10:23:54' is a timestamp without time zone, while TIMESTAMP '2004-10-19 10:23:54+02' is a timestamp with time zone. PostgreSQL never examines the content of a literal string before determining its type, and therefore will treat both of the above as timestamp without time zone. To ensure that a literal is treated as timestamp with time zone, give it the correct explicit type: TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'

In a value that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived from the date/time fields in the input string, and is not adjusted for time zone.

For timestamp with time zone values, an input string that includes an explicit time zone will be converted to UTC (Universal Coordinated Time) using the appropriate offset for that time zone. If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's (see guc-timezone) parameter, and is converted to UTC using the offset for the timezone zone. In either case, the value is stored internally as UTC, and the originally stated or assumed time zone is not retained.

When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone. To see the time in another time zone, either change timezone or use the AT TIME ZONE construct (see (see postgresql_16:3.6.9.4)).

Conversions between timestamp without time zone and timestamp with time zone normally assume that the timestamp without time zone value should be taken or given as timezone local time. A different time zone can be specified for the conversion using AT TIME ZONE.

Special Values

PostgreSQL supports several special date/time input values for convenience, as shown in (see postgresql_16:3.5.5.1). The values infinity and -infinity are specially represented inside the system and will be displayed unchanged; but the others are simply notational shorthands that will be converted to ordinary date/time values when read. (In particular, now and related strings are converted to a specific time value as soon as they are read.) All of these values need to be enclosed in single quotes when used as constants in SQL commands.

| Input String | Valid Types | Description | | --- | --- | --- | | epoch | date, timestamp | 1970-01-01 00:00:00+00 (Unix system time zero) | | infinity | date, timestamp | later than all other time stamps | | -infinity | date, timestamp | earlier than all other time stamps | | now | date, time, timestamp | current transaction's start time | | today | date, timestamp | midnight (00:00) today | | tomorrow | date, timestamp | midnight (00:00) tomorrow | | yesterday | date, timestamp | midnight (00:00) yesterday | | allballs | time | 00:00:00.00 UTC |

The following SQL-compatible functions can also be used to obtain the current time value for the corresponding data type: CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, LOCALTIME, LOCALTIMESTAMP. (See (see postgresql_16:3.6.9.5).) Note that these are SQL functions and are *not* recognized in data input strings.

Caution

While the input strings now, today, tomorrow, and yesterday are fine to use in interactive SQL commands, they can have surprising behavior when the command is saved to be executed later, for example in prepared statements, views, and function definitions. The string can be converted to a specific time value that continues to be used long after it becomes stale. Use one of the SQL functions instead in such contexts. For example, CURRENT_DATE + 1 is safer than 'tomorrow'::date.

Back