Discarding Timestamp Milliseconds and Seconds
How can you eliminate the millisecond and second components from a timestamp without using a timezone?
Discarding Using Cast
Casting a timestamp to timestamp(0) or timestamptz(0) will round the value to the nearest whole second. For example:
SELECT now()::timestamp(0);
Truncating Using date_trunc()
The date_trunc() function can truncate a timestamp, leaving the seconds unchanged. This is often a more desirable option than rounding. For example:
SELECT date_trunc('second', now()::timestamp);
Syntax
date_trunc accepts input for the first argument "field". Use the following values to truncate:
Output
The data type of the return value will match the input data type (timestamp, timestamptz, or interval).
The above is the detailed content of How to Discard Milliseconds and Seconds from a Timestamp Without Time Zones?. For more information, please follow other related articles on the PHP Chinese website!