Find timestamp difference in day in kdb

kdb q-lang

932 观看

2回复

1625 作者的声誉

How do you find the time difference in days between 2 timestamp in KDB?

For eg.

q)d1:2019.02.16D12:16:00.000
q)d2:.z.P

how to find difference between these two timestamp in days?

q)d1-d2 //in day
作者: HadeS 的来源 发布者: 2017 年 12 月 27 日

回应 2


3

131 作者的声誉

决定

You can cast to the date datatype and subtract:

(`date$d1)-`date$d2

or

d1.date - d2.date

https://code.kx.com/q/ref/casting/#cast

作者: wpc135 发布者: 2017 年 12 月 27 日

1

3569 作者的声誉

Depending on the accuracy you require you could simply do (`date$d1)-`date$d2 to check the difference of dates in days. If you are concerned with exact periods of 24 hours then the logic must be extended to include the time elements of the timestamp:

((`date$d1)-`date$d2)-0>(`time$d1)-`time$d2

Which checks to see if 24 hours have passed on the last day.

Alternatively you can compare the number of 24 hour increments in each timestamp by dividing by 1D00:00 and using floor; as in the following function:

{floor(x-y)%1D00:00}[d1;d2]
作者: Thomas Smyth 发布者: 2017 年 12 月 28 日
32x32