Well if you don't mind a little bit of handywork, you can get along fine with the core module Time::Local. That module implements the functions timelocal() and timegm(), the inverse functions of respectively localtime() and gmtime().

One thing you should not forget, is to decrement the month value: January is month 0, December month 11. 4-digit years are fine, though you're allowed to subtract 1900.

Here's some sample code, that calculates the time value for some date, at noon, GMT:

my $datestring = "2003/11/19"; # input use Time::Local; my($y, $m, $d) = $datestring =~ m<^(\d+)/(\d+)/(\d+)$> or die "Not a valid date"; my $time = timegm(0, 0, 12, $d, $m-1, $y); print "The time value is $time, for " . gmtime($time) . "\n";
Result:
The time value is 1069243200, for Wed Nov 19 12:00:00 2003

p.s. The results are limited to the date range that applies to Unix time, thus to localtime and gmtime, i.e. from 1970 to 2035, roughly; or the equivalent on your platform — on the Mac, that's from 1904 to around 2035.


In reply to Re: Converting string to date by bart
in thread Converting string to date by marctwo

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.