Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found