如何只计算两个日期之间的日期在postgres sql查询中。
假设我给了两个日期,如果相差一个月,那么它应该显示为30天。即使是几个月,也需要将我尝试过使用age(date,now():: timestamp的天数
转换为天数没有时区),但给出的是月-日期-年格式。例如:10周一24天13:57:40.5265。但是我需要遵循以下方式。
例如,如果相差两个月,那么我需要60天的输出。
Suppose I have given two dates, if difference is one month then it should be display as 30 days.Even months also need to convert into days I have tried with age( date,now()::timestamp without time zone) but it is giving months-dates-years format. ex: 10 mons 24 days 13:57:40.5265.But I need following way. For example if two months difference is there then I need output as 60 days.
不要使用 age()
函数用于日期/时间算术。它仅返回符号结果(足够好了用于人类表示,但对于日期/时间计算几乎没有意义;与标准差相比)。
Don't use the age()
function for date/time arithmetic. It only returns "symbolic" results (which are good enough for human representation, but almost meaningless for date/time calculations; compared to the standard difference).
标准差异运算符(-
)返回两者的基于天的结果日期
,时间戳
和带时区的时间戳
(前者返回天为 int
,后两个返回天为基础的 interval
s):
The standard difference operator (-
) returns day-based results for both date
, timestamp
and timestamp with time zone
(the former returns days as int
, the latter two return day-based interval
s):
从基于天的间隔中,您可以使用 extract()
函数提取日期:
From the day-based intervals you can extract days with the extract()
function:
select current_date - '2017-01-01',
extract(day from now()::timestamp - '2017-01-01 00:00:00'),
extract(day from now() - '2017-01-01 00:00:00Z');