Postgres: how do you round a timestamp up or down to the nearest minute?

前端 未结 4 1288
星月不相逢
星月不相逢 2020-12-14 13:37

Is there a postgresql function that will return a timestamp rounded to the nearest minute? The input value is a timestamp and the return value should be a timestamp.

4条回答
  •  眼角桃花
    2020-12-14 14:11

    Use the built-in function date_trunc(text, timestamp), for example:

    select date_trunc('minute', now())
    

    Edit: This truncates to the most recent minute. To get a rounded result, add 30 seconds to the timestamp first, for example:

    select date_trunc('minute', now() + interval '30 second')
    

    This returns the nearest minute.

    See Postgres Date/Time Functions and Operators for more info

提交回复
热议问题