Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Date format YYYY-MM-DD HH-MM-SS

by Rahul Gupta (Sexton)
on Oct 13, 2013 at 10:29 UTC ( #1058052=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Date format YYYY-MM-DD HH-MM-SS
Download Code
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 (Abbot) 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 (Deacon) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1058052]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2015-07-04 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls