http://www.perlmonks.org?node_id=1058052

Rahul Gupta has asked for the wisdom of the Perl Monks concerning the following question:

How can i get date in this format

YYYY-MM-DD HH-MM-SS

Thanks in advance

Replies are listed 'Best First'.
Re: Date format YYYY-MM-DD HH-MM-SS
by moritz (Cardinal) on Oct 13, 2013 at 10:32 UTC
      For *nix like systems the lame but the fast way is my $date = `date +"%Y-%m-%d-%H-%M-%S"`;
Re: Date format YYYY-MM-DD HH-MM-SS
by keszler (Priest) on Oct 13, 2013 at 14:50 UTC
    In the spirit of Tim Toady, when you have all the date/time elements in separate variables printf / sprintf can be your friend:
    use strict; use warnings; my $format = "%4u-%02u-%02u %02u-%02u-%02u"; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; printf "$format\n", $year+1900, $mon+1, $mday, $hour, $min, $sec; my $event = 'The Eagle has landed (' . sprintf($format, 1969, 7, 20, 2 +0, 17, 40) . " UTC)\n"; print $event; __END__ 2013-10-13 10-49-01 The Eagle has landed (1969-07-20 20-17-40 UTC)
Re: Date format YYYY-MM-DD HH-MM-SS
by Lennotoecom (Pilgrim) on Oct 13, 2013 at 17:18 UTC
    one of the ways to go
    if you wish get your data into
    the separate variables
    @date = (localtime(time))[5, 4, 3, 2, 1, 0]; $date[0]+=1900; $date[1]++; printf "%4d-%02d-%02d %02d:%02d:%02d\n", @date;
Re: Date format YYYY-MM-DD HH-MM-SS
by Happy-the-monk (Canon) on Oct 13, 2013 at 18:07 UTC

    How can i get date in this format

    YYYY-MM-DD HH-MM-SS

    perl -E'use Time::Piece::ISO; ($t=localtime) =~s/T/ /; $t =~s/:/-/g; say $t'

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: Date format YYYY-MM-DD HH-MM-SS
by BillKSmith (Monsignor) on Oct 13, 2013 at 20:45 UTC
    If you have the data in the form returned by localtime, you can format it with the function strftime in POSIX.
    Bill