Skip to content

Formats for Datetime Values

The following formats (datetime masks) are supported in arguments to the following functions:

FormatDescription
HH, HH12Hour of day (01-12)
HH24Hour of day (00-23)
MIMinute (00-59)
SSSecond (00-59)
MSMillisecond (000-999)
USMicrosecond (000000-999999)
SSSSSeconds past midnight (0-86399)
AM, am, PM, pm, A.M., a.m., P.M., p.m.Meridiem indicator (with and without periods)
Y,YYY4-digit year with comma
YYYY, YYY, YY, Y4-digit year, last 3, last 2, last 1
IYYY, IYY, IY, IISO 8601 week-numbering year (4 digits, last 3, last 2 last 1)
BC, bc, AD, ad, B.C., b.c., A.D., a.d.Era indicator (with and without periods)
MONTH, Month, monthUppercase, capitalized, or lowercase month name (blank-padded to 9 characters)
MON, Mon, monAbbreviated uppercase, capitalized, or lowercase month name (3 characters in English)
MMMonth number (01-12)
DAY, Day, dayUppercase, capitalized, or lowercase day name (blank-padded to 9 characters)
DY, Dy, dyAbbreviated uppercase, capitalized, or lowercase day name (3 characters in English)
DDDDay of year (001-366)
IDDDDay of ISO 8601 week-numbering year (001-371; day 1 of the year is Monday of the first ISO week)
DDDay of month (01-31)
DDay of the week, Sunday (1) to Saturday (7)
FMFill mode prefix (a pattern modifier that suppresses trailing zeroes and padding blanks)
FXThis prefix must be the first item in the format string. Normally, TO_TIMESTAMP and TO_DATE functions ignore multiple blank spaces. If you specify the FX prefix, these functions do not ignore blank spaces. For example: FX Month DD Day. This prefix is not supported with the TO_CHAR function.
IDISO 8601 day of the week, Monday (1) to Sunday (7)
WWeek of month (1-5) (the first week starts on the first day of the month)
WWWeek number of year (1-53) (the first week starts on the first day of the year)
IWWeek number of ISO 8601 week-numbering year (01-53; the first Thursday of the year is in week 1)
CCCentury (2 digits) (the twenty-first century starts on 2001-01-01). The CC pattern is ignored when the year is specified.
JJulian Day (integer days since November 24, 4714 BC at midnight UTC) Not supported by TO_CHAR.
QQuarter (ignored by TO_DATE and TO_TIMESTAMP)
RM, rmMonth in uppercase or lowercase Roman numerals (I-XII and I=January or i-xii and i=January)Not supported by TO_CHAR.

TH, thOrdinal number suffix (a pattern modifier). For example: DDth or DDTH
TZ, tzUppercase or lowercase time-zone name (the long form of the name. For example: PST8PDT is returned, not PST.
OFTime-zone offsetNot supported by TO_CHAR.

To convert Julian dates to the yyyy-mm-dd format, store the Julian dates as VARCHAR with a J preceeding the value. For example:

premdb=# create table yb100 (julian varchar(10));
CREATE TABLE

premdb=# insert into yb100 values ('J2456783');
INSERT 0 1

premdb=# SELECT julian::DATE from yb100;
   julian   
------------
 2014-05-05
(1 row)

Using the same table, Julian dates can be converted to the yyyy-mm-dd format using the TO_DATE function:

premdb=# SELECT TO_DATE(2456783::text, 'J');
  to_date   
------------
 2014-05-05
(1 row)

Separators in Date Formats

The basic date formats work with or without separator characters and with uppercase and lowercase letters; however, the literal date values and the corresponding date masks must match. Do not specify separators in the mask that do not exist in the input string, and do not use additional separator characters, such as extra spaces or hyphens, between format elements unless the input string also contains those characters.

For example, the following date masks are valid for a date such as 30 May 2016:

DD Mon YYYY
dd mon yyyy

However, the following masks will fail for the same input string:

DD-Mon-YYYY
DDMonYYYY
ddmonyyyy
dd  mon  yyyy

Year Formats

Do not use a four-digit year in the mask if the input string contains fewer digits. For example:

premdb=# SELECT TO_DATE('30 May 16 ', 'dd mon yyyy') from sys.const;
  to_date   
------------
 0016-05-30
(1 row)

The correct mask in this case would be dd mon yy.

Conversely, if the input value contains a four-digit year, the mask may specify YYYY, YYY, YY, or Y. For example:

premdb=# SELECT TO_DATE('30-May-2016', 'DD-Mon-Y') from sys.const;
  to_date   
------------
 2016-05-30
(1 row)

Handling of US Format (Microseconds)

The TO_TIMESTAMP function ignores any leading zeroes that appear in the format for microseconds. For example:

premdb=# select to_timestamp('20180228 11 59 59 000999 AM', 'YYYYMMDD HH12 MI SS US AM') from sys.const;
       to_timestamp        
----------------------------
 2018-02-28 11:59:59.999-08
(1 row)

Parent topic:Formatting Functions