获取该日期所属季度前一季度的最后一天的日期

che*_*nyf 6 date raku quarter

给定一个日期,返回该日期所属季度前一个季度的最后一天的日期。例如

2020-04-25 => 2020-03-31
2020-06-25 => 2020-03-31
2020-09-25 => 2020-06-30
2020-10-25 => 2020-09-30
Run Code Online (Sandbox Code Playgroud)

如果给定日期在第一季度,则年份减去 1

2020-03-25 => 2019-12-31
Run Code Online (Sandbox Code Playgroud)

Eli*_*sen 9

sub MAIN(Date(Str) $date) {
    say $date.earlier(months => ($date.month - 1) % 3 + 1).last-date-in-month
}
Run Code Online (Sandbox Code Playgroud)

这至少需要 Rakudo 2020.05。

  • [last-date-in-month](https://docs.raku.org/routine/last-date-in-month) 方法很酷。 (2认同)
  • @psteve:这是一种[强制类型](https://docs.raku.org/type/Signature#index-entry-Coercion_type)。基本上,如果参数是“Str”,则将对其调用“.Date”。也许更广为人知的是:“Int(Str)”,如果它是“Str”,则强制转换为“Int”。或者更短的形式“Int()”,它是“Int(Any)”的缩写:如果它是“Any”,则强制转换为“Int”。 (2认同)