Today's Date in Perl in MM/DD/YYYY format

后端 未结 7 1219
醉酒成梦
醉酒成梦 2020-12-24 11:18

I\'m working on a Perl program at work and stuck on (what I think is) a trivial problem. I simply need to build a string in the format \'06/13/2012\' (always 10 characters,

7条回答
  •  没有蜡笔的小新
    2020-12-24 11:39

    You can use Time::Piece, which shouldn't need installing as it is a core module and has been distributed with Perl 5 since version 10.

    use Time::Piece;
    
    my $date = localtime->strftime('%m/%d/%Y');
    print $date;
    

    output

    06/13/2012
    


    Update

    You may prefer to use the dmy method, which takes a single parameter which is the separator to be used between the fields of the result, and avoids having to specify a full date/time format

    my $date = localtime->dmy('/');
    

    This produces an identical result to that of my original solution

提交回复
热议问题