As already mentioned, the Date::Calc module will do the trick. Specifically, look at the Decode_Date_US and Decode_Date_EU functions for parsing out the day, month, and year. Unless I'm mistaken, though, you'll have to employ something like sprintf to format the date. There is also the check_date function to verify the date is valid.

In the spirit of TMTOWTDI, though, you could also use the Date::Manip module. It has different strengths than Date::Calc, and it might be useful for other projects. Here is how you could do this using Date::Manip:

use strict; use warnings; use Date::Manip; my $rawdate = '10/12/04'; Date_Init( "DateFormat = US" ); # default munge_date( $rawdate ); Date_Init( "DateFormat = European" ); munge_date( $rawdate ); $rawdate = '32/12/04'; munge_date( $rawdate ); sub munge_date { my ( $rawdate ) = @_; my %cfgvars = map { split( '=', $_ ) } Date_Init(); print "DateFormat is set to $cfgvars{DateFormat}, ", "parsing $rawdate:\n"; my $date = DateCalc( $rawdate, "+ 1 week" ); if( not defined $date ) { print " $rawdate is not a valid date\n"; return; } print ' orig date = ', UnixDate( $rawdate, "%b %e, %Y" ), "\n"; print ' + 1 week = ', UnixDate( $date, "%m/%d/%y" ), " (mm/dd/yy)\n"; print ' ', UnixDate( $date, "%d/%m/%y" ), " (dd/mm/yy)\n"; print ' ', UnixDate( $date, "%Y-%m-%d" ), " (yyyy-mm-dd)\n"; } ** OUTPUT ** DateFormat is set to US, parsing 10/12/04: orig date = Oct 12, 2004 + 1 week = 10/19/04 (mm/dd/yy) 19/10/04 (dd/mm/yy) 2004-10-19 (yyyy-mm-dd) DateFormat is set to European, parsing 10/12/04: orig date = Dec 10, 2004 + 1 week = 12/17/04 (mm/dd/yy) 17/12/04 (dd/mm/yy) 2004-12-17 (yyyy-mm-dd) DateFormat is set to European, parsing 32/12/04: 32/12/04 is not a valid date
If you use Date::Manip, make sure to set a default value for the timezone variable. If the timezone cannot be determined automatically, you'll get an error.


In reply to Re: Date module laziness by bobf
in thread Date module laziness by Cody Pendant

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.