Postgres:如何将时间戳向上或向下舍入到最近的分钟? [英] Postgres: how do you round a timestamp up or down to the nearest minute?

查看:109
本文介绍了Postgres:如何将时间戳向上或向下舍入到最近的分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个postgresql函数将返回四舍五入到最接近分钟的时间戳?输入值是一个时间戳记,返回值应该是一个时间戳记。

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.

推荐答案

使用内置函数 date_trunc(text,timestamp),例如:

select date_trunc('minute', now())

编辑:最近分钟。要获得四舍五入的结果,请先将时间戳记添加30秒,例如:

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')

这将返回最接近分钟。

请参见 Postgres日期/时间函数和运算符以获取更多信息

这篇关于Postgres:如何将时间戳向上或向下舍入到最近的分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆