日期之间每 6 个月列出一次

问题描述 投票:0回答:1

我有一个函数,可以使用 `til

列出开始日期和结束日期之间的日期

我正在尝试找到一些东西(也许使用“xbar”),它将列出从开始日期起 6 个月间隔内的每个日期

例如。 2021.02.15 2021.07.15 2022.02.15等

我集成了 Q.addmonths[],但似乎无法将它们放在一起

kdb q
1个回答
0
投票

这样的东西对你有用吗?

q){(-1+`dd$x)+`date$(`month$x)+y*til z}[2021.02.15;6;4]
2021.02.15 2021.08.15 2022.02.15 2022.08.15

它以

z
参数给定的间隔生成由
y
参数指定的月数。
x
是您的开始日期。

`month$x - pulls out the month of your date
`dd$x - pulls out the day of the month

您也可以像您提到的那样使用

.Q.addmonths
(相同的参数):

q){.Q.addmonths[x;y*til z]}[2021.02.15;6;4]
2021.02.15 2021.08.15 2022.02.15 2022.08.15

对于这两种解决方案,请记住https://code.kx.com/q/ref/dotq/#addmonths:

文档中的警告
If the date x is near the end of the month and (x.month + y)’s month has fewer days than x.month, the result may spill over to the following month.
© www.soinside.com 2019 - 2024. All rights reserved.