How can I change the date formats in Perl?

前端 未结 5 1797
生来不讨喜
生来不讨喜 2020-12-07 02:16

I just want to convert the dates from 20111230 format to 30-dec-2011.

5条回答
  •  温柔的废话
    2020-12-07 02:48

    If I can't use one of the date modules, POSIX isn't so bad and it comes with perl:

    use v5.10;
    use POSIX qw(strftime);
    
    my $date = '19700101';
    
    my @times;
    @times[5,4,3] = $date =~ m/\A(\d{4})(\d{2})(\d{2})\z/;
    $times[5] -= 1900;
    $times[4] -= 1;
    
    # strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1)
    say strftime( '%d-%b-%Y', @times );
    

    Making @times is a bit ugly. You can't always get what you want, but if you try sometimes, you might find you get what you need.

提交回复
热议问题