想要将R中的日期四舍五入到最近的季度末?

时间:2019-03-18 16:08:44

标签: r vba date

我已经在Excel中的VBA中完成了此操作,它将日期四舍五入到最接近的季度结束日期。在R中有简单的方法可以做到这一点吗?

如果没有,这是我的VBA代码,是否可以将其转换为R?该代码循环遍历“文件”标签上的每个项目,并将其日期转换为季度结束日期。

Dim AppDate As Date
Dim MidQuarter As Date
Dim Dte As Date

y = 3
Sheets("File").[B1].Formula = "=Counta(A:A)"
Num_Files = Sheets("File").Range("B1").Value
ReportDate = “12/31/2018”

Do Until y > Num_Files
Dte = Worksheets("File").Cells(y, 27)

'this section turns Dte into the last day of the nearest quarter.
MidQuarter = Int(Application.Average(DateAdd("q", Format(Dte, "q"), "1/1/" & 
Year(Dte)), DateAdd("q", Format(Dte, "q") - 1, "1/1/" & Year(Dte))))

AppDate = DateSerial(Year(Dte), 3 * (Format(Dte, "q") + (Dte < MidQuarter)) + 1, 0)
Worksheets("File").Cells(y, 78).Value = AppDate
y = y + 1
Loop

有人问过类似的问题,但只是隐瞒季度日期。四舍五入到最近的四分之一没有帮助。
这是我要寻找的示例:

  • 如果日期为01/15/2018,则将四舍五入到12/31/2017。
  • 如果日期为02/10/2018,则将四舍五入到12/31/2017。
  • 如果日期为2018年2月25日,则将四舍五入到2018年3月31日。

0 个答案:

没有答案