Questions And Answers

More Tutorials

postgreSQL Dates Timestamps and Intervals


Cast a timestamp or interval to a string

You can convert a timestamp or interval value to a string with the to_char() function:

SELECT to_char('2016-08-12 16:40:32'::timestamp, 'DD Mon YYYY HH:MI:SSPM');

This statement will produce the string "12 Aug 2016 04:40:32PM". The formatting string can be modified in many different ways; the full list of template patterns can be found here.

Note that you can also insert plain text into the formatting string and you can use the template patterns in any order:

SELECT to_char('2016-08-12 16:40:32'::timestamp,
 '"Today is "FMDay", the "DDth" day of the month of "FMMonth" of "YYYY');

This will produce the string "Today is Saturday, the 12th day of the month of August of 2016". You should keep in mind, though, that any template patterns - even the single letter ones like "I", "D", "W" - are converted, unless the plain text is in double quotes. As a safety measure, you should put all plain text in double quotes, as done above.

You can localize the string to your language of choice (day and month names) by using the TM (translation mode) modifier. This option uses the localization setting of the server running PostgreSQL or the client connecting to it.

SELECT to_char('2016-08-12 16:40:32'::timestamp, 'TMDay, DD" de "TMMonth" del año "YYYY');

With a Spanish locale setting this produces "Sábado, 12 de Agosto del año 2016".


In this page (written and validated by ) you learned about postgreSQL Dates Timestamps and Intervals . What's Next? If you are interested in completing postgreSQL tutorial, your next topic will be learning about: postgreSQL SELECT the last day of month.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.