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

时间:2012-02-23 23:25:13

标签: perl datetime

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

2 个答案:

答案 0 :(得分:12)

以下是仅使用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";

答案 1 :(得分:11)

尝试:

use Date::Calc qw(:all);

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