The secret of dealing with dates in Perl is to convert your value into an "epoch seconds" value. You can do that using timelocal from Time::Local. In your case, it would be done like this (assuming your data line is in $_):

use Time::Local; # get date and time my ($date, $time) = (split /\|/)[4, 5]; # get dmy my ($d,$m,$y) = split /\//, $date; # get hours & mins my ($hr, $min) = split /:/, $time; # convert year and month into 'correct' forms $y -= 1900; --$m; # get epoch (use 0 for seconds) my $epoch = timelocal(0, $min, $hr, $d, $m, $y);

The easiest way to get formatted dates from epoch seconds is to use POSIX::strftime, like this:

use POSIX 'strftime'; my $fmt_date = strftime('%Y-%m-%d', localtime($epoch));

Please don't use Date::Manip unless you can't find any other way to do what you want. It's a huge inefficient module and using it will really slow your script down.

--
<http://www.dave.org.uk>

Perl Training in the UK <http://www.iterative-software.com>


In reply to Re: Changing a Date format by davorg
in thread Changing a Date format by Perl Newby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.