PostgreSQL TIMEOFDAY() Function
Summary: in this tutorial, you will learn how to use the PostgreSQLTIMEOFDAY() function to retrieve the current date and time as a formatted string.
Introduction to the PostgreSQL TIMEOFDAY() function
TheTIMEOFDAY() function returns thecurrent date and time as a formatted string.
Here’s the syntax of theTIMEOFDAY() function:
TIMEOFDAY()The function does not have any parameters and returns the current date and time as a string.
Note that theTIMEOFDAY() function returns the same result as theCLOCK_TIMESTAMP() function but in the text string.
PostgreSQL TIMEOFDAY() function examples
Let’s take some examples of using theTIMEOFDAY() function.
1) Basic TIMEOFDAY() function example
The following example uses theTIMEOFDAY() function to retrieve the current date and time as a string:
timeofday------------------------------------- Wed Mar 20 10:20:10.108369 2024 -07(1 row)The output shows the date, time, and timezone.
2) Formatting the output
If you want a specific format, you can cast the result of theTIMEOFDAY() function into a timestamp and use theto_char() function to achieve the desired format:
SELECT to_char( timeofday()::timestamp, 'YYYY-MM-DD HH24:MI:SS' ) current_time;Output:
current_time--------------------- 2024-03-20 10:26:57(1 row)Summary
- Use the
TIMEOFDAY()function to obtain the current date and time as a formatted string.
Last updated on