Normally, the best thing to do would be to work directly with the database's date/time functions.

Perl's DateTime's "now" feature is in this format

Not exactly, that's just the default stringification. Note that DateTime objects can be compared directly, they overload the comparison operators.

If you're really stuck with parsing a string, my personal favorite module is DateTime::Format::Strptime. Here's one way to go about it:

use warnings; use strict; use DateTime; use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new(on_error=>'croak', pattern => '%a %b %d, %Y - %H:%M:%S', time_zone => 'UTC'); my $dt = $strp->parse_datetime('Tue Oct 23, 2018 - 19:57:12'); print $dt->strftime('%Y-%m-%d %H:%M:%S %z'), "\n"; my $dt_one_day_ago = DateTime->now->subtract(days=>1); print "$dt is ", $dt < $dt_one_day_ago ? '<' : '>=', " $dt_one_day_ago\n";

The above takes the time of day into account as well, if that's what you want. Also, make sure to set the time zones correctly!

(BTW, there is also DateTime::Format::MySQL and DateTime::Format::DBI, although I haven't used these myself.)


In reply to Re: Comparing different timestamp formats by haukex
in thread Comparing different timestamp formats by htmanning

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.