Perl:给定年份和周数,我怎样才能获得该周的第一个日期?

发布于 2025-01-08 05:23:27 字数 145 浏览 0 评论 0原文

使用优秀的 Perl DateTime 模块,获取日期的年份和周数很简单,但是走另一条路似乎有点困难。如何获取以年份和周数开头的日期?

Using the excellent Perl DateTime module it is trivial to obtain the year and week number for a date, but going the other way seems to be a bit more difficult. How does one go about obtaining a date starting with the year and week number?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2025-01-15 05:23:27

这是仅使用 DateTime 执行此操作的一种方法:

use DateTime;

sub first_day_of_week
{
  my ($year, $week) = @_;

  # Week 1 is defined as the one containing January 4:
  DateTime
    ->new( year => $year, month => 1, day => 4 )
    ->add( weeks => ($week - 1) )
    ->truncate( to => 'week' );
} # end first_day_of_week


# Find first day of second week of 2012 (2012-01-09):
my $d = first_day_of_week(2012, 2);

print "$d\n";

Here's one way to do it using only DateTime:

use DateTime;

sub first_day_of_week
{
  my ($year, $week) = @_;

  # Week 1 is defined as the one containing January 4:
  DateTime
    ->new( year => $year, month => 1, day => 4 )
    ->add( weeks => ($week - 1) )
    ->truncate( to => 'week' );
} # end first_day_of_week


# Find first day of second week of 2012 (2012-01-09):
my $d = first_day_of_week(2012, 2);

print "$d\n";
夏了南城 2025-01-15 05:23:27

尝试:

use Date::Calc qw(:all);

my $year = 2012;
my $week = 14;
my ($year2, $month, $day) = Monday_of_Week($week, $year);

Try:

use Date::Calc qw(:all);

my $year = 2012;
my $week = 14;
my ($year2, $month, $day) = Monday_of_Week($week, $year);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文