获取该日期所在季度之前的季度的最后一天的日期
Get the date of the last day of the quarter preceding the quarter in which this date falls
给定一个日期,return 该日期所在季度的前一个季度的最后一天的日期。例如
2020-04-25 => 2020-03-31
2020-06-25 => 2020-03-31
2020-09-25 => 2020-06-30
2020-10-25 => 2020-09-30
如果给定日期在第一季度,则年份减去 1
2020-03-25 => 2019-12-31
sub MAIN(Str $day) {
my Date $d = Date.new($day);
my Int $year = $d.year;
my Int $month = $d.month;
my Int $quarter = ($month/3).ceiling; # the quarter that this date falls
my @last-days = ('12-31', '03-31', '06-30', '09-30');
# if the given date is in the first quarter, then `$year` minus 1
$year-=1 if $quarter == 1;
say sprintf('%s-%s', $year, @last-days[$quarter-1]);
}
将上面的代码保存为quarter.raku
,输出为:
$ raku quarter.raku 2020-03-25
2019-12-31
$ raku quarter.p6 2020-04-25
2020-03-31
$ raku quarter.p6 2020-06-25
2020-03-31
$ raku quarter.p6 2020-07-25
2020-06-30
$ raku quarter.p6 2020-08-25
2020-06-30
$ raku quarter.p6 2020-09-25
2020-06-30
$ raku quarter.p6 2020-10-25
2020-09-30
sub MAIN(Date(Str) $date) {
say $date.earlier(months => ($date.month - 1) % 3 + 1).last-date-in-month
}
这至少需要 Rakudo 2020.05。
给定一个日期,return 该日期所在季度的前一个季度的最后一天的日期。例如
2020-04-25 => 2020-03-31
2020-06-25 => 2020-03-31
2020-09-25 => 2020-06-30
2020-10-25 => 2020-09-30
如果给定日期在第一季度,则年份减去 1
2020-03-25 => 2019-12-31
sub MAIN(Str $day) {
my Date $d = Date.new($day);
my Int $year = $d.year;
my Int $month = $d.month;
my Int $quarter = ($month/3).ceiling; # the quarter that this date falls
my @last-days = ('12-31', '03-31', '06-30', '09-30');
# if the given date is in the first quarter, then `$year` minus 1
$year-=1 if $quarter == 1;
say sprintf('%s-%s', $year, @last-days[$quarter-1]);
}
将上面的代码保存为quarter.raku
,输出为:
$ raku quarter.raku 2020-03-25
2019-12-31
$ raku quarter.p6 2020-04-25
2020-03-31
$ raku quarter.p6 2020-06-25
2020-03-31
$ raku quarter.p6 2020-07-25
2020-06-30
$ raku quarter.p6 2020-08-25
2020-06-30
$ raku quarter.p6 2020-09-25
2020-06-30
$ raku quarter.p6 2020-10-25
2020-09-30
sub MAIN(Date(Str) $date) {
say $date.earlier(months => ($date.month - 1) % 3 + 1).last-date-in-month
}
这至少需要 Rakudo 2020.05。