#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    How to convert datetime to a yyyy-mm-dd hh:mm:ss format in perl?


    How do I convert datetime to a different format? This is my date: Tue Feb 11 08:47:59 2014 how to convert to 2014-02-11 08:47:59


    Thanks!
    andrew
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Well it depends. Your date looks like a Unix print-out format. If you obtained this date with the time Perl function, then this function really returns a number, i.e. the number of seconds elapsed since the epoch (1-Jan-1970 on Unix systems). This number can be converted into an array containong the various element of the date with the gmtime and localtime functions. This array contains:
    - 0 : seconds
    - 1 : minutes
    - 2 : hours
    - 3 : day in month
    - 4 : month (Jan = 0, Feb = 1, etc.)
    - 5 : years elapsed sin 1900,
    - 6 : day of week (0 = Sunday)
    etc.

    The Time::localtime module makes it possible to access these elements by name, if you prefer.

    But if you have the string "Tue Feb 11 08:47:59 2014", then you probably have to use regexes to reformat your date to your target format (although there may possibly be a module doing it, I don't know).
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Try using Date::Parse perhaps.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2005
    Posts
    505
    Rep Power
    119
    Code:
    my %month;
    @month{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)}=(1..12);
    my $date="Tue Feb 11 08:47:59 2014";
    $date=~s{^([a-zA-z]{3}) ([a-zA-z]{3}) (\d\d) (\d\d:\d\d:\d\d) (\d\d\d\d)$}{$5-$month{$2}-$3 $4};
    "Plagiarize the code of others, I say!"

    That's about the worst you can do. It's the fast way to make a very bad coder out of you. It's important to understand what you are doing. Just asking what you should do, and blindly copying that if you would have gotten an unambigious answer is very, very bad.

    -- Abigail
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Use the POSIX module and in particular the strftime routine.

IMN logo majestic logo threadwatch logo seochat tools logo