How can I get this week's dates in Perl?

后端 未结 4 1798
感情败类
感情败类 2021-02-09 04:44

I have the following loop to calculate the dates of the current week and print them out. It works, but I am swimming in the amount of date/time possibilities in Perl and want t

4条回答
  •  無奈伤痛
    2021-02-09 05:06

    This should work:

    use POSIX; # for strftime
    my $time = time ();
    my $seconds = 24*60*60;
    my @time = gmtime ();
    $time = $time - $time[6] * $seconds;
    for my $wday (0..6) {
        $time += $seconds;
        my @wday = gmtime ($time);
        print strftime ("%A %d %B %Y\n", @wday);
    }
    

    Gives me:

    $ ./week.pl 
    Monday 24 May 2010
    Tuesday 25 May 2010
    Wednesday 26 May 2010
    Thursday 27 May 2010
    Friday 28 May 2010
    Saturday 29 May 2010
    Sunday 30 May 2010
    

    If you want to get weeks starting on Sunday, change $time[6] to ($time[6] + 1).

    This assumes you want the GMT weeks. Change gmtime to localtime to get local time zone weeks.

提交回复
热议问题