计算下一个星期一到给定日期的日期 [英] calculate date of next monday to a given date
本文介绍了计算下一个星期一到给定日期的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望返回给定日期的下一个星期一的日期.如果给定的日期已经是星期一,则该日期应保持不变.
I am looking to return the date of the next monday of a given date. In case the given date is already a monday the date should remain the same.
在另一篇文章中,我找到了一个脚本,该脚本可以使用DateTime计算给定日期的上一个星期一.
From another post I found a script to calculate the previous monday of a given date using DateTime.
use DateTime;
my $date = DateTime->new(year => 2011, month => 6, day => 11);
my $desired_dow = 1; # Monday
$date->subtract(days => ($date->day_of_week - $desired_dow) % 7);
print "$date\n";
(贷方为cjm)
我根本无法找出如何修改此值以计算下一个星期一(而不是前一个星期一).有人可以帮忙吗?
I simply cannot find out how to modifiy this in order to calculate the next monday (and not the previous one). Can someone help?
推荐答案
更改
$date->subtract(days => ($date->day_of_week - $desired_dow) % 7);
到
$date->add(days => ($desired_dow - $date->day_of_week) % 7);
或者您可以选择将一周添加到您的旧"星期一:
or you could have optionally just added a week to your "old" Monday:
$date->subtract(days => ($date->day_of_week - $desired_dow) % 7);
$date->add(days => 7);
这篇关于计算下一个星期一到给定日期的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文