How to compare Date fields in perl

Posted on 2007-10-13
Last Modified: 2010-07-04
In a Perl script, I'm selecting records from database, some of the records are Date fields, Selected using to_char in "dd-mon-yyyy hh:24mi:ss" format.
I need to compare between another date field to check if one date is older than the other, how do i do that
Question by:prasen120998
    LVL 84

    Expert Comment

    (join'',(split/[- ]/,$one)[2,1,0,3]) cmp join'',(spit/[- ]/,$another)[2,1,0,3]
    LVL 84

    Expert Comment

    (join'',(split/[- ]/,$one)[2,1,0,3]) cmp join'',(split/[- ]/,$another)[2,1,0,3]

    Author Comment

    Hi ozo, the above solution is not working.
    I've two date fields, DBDate as say 02-NOV-2005 21:05:22
    and sysdate as 13-OCT-2007 10:25:15
    How do I check to see if sysdate is greater or less than DBdate

    I can check by converting them as yyyymmddhh24miss, but then its tedious, is there anything simpler
    LVL 84

    Expert Comment

    $DBDate = "02-NOV-2005 21:05:22";
    $sysdate = "13-OCT-2007 10:25:15";
    my %mon;
    @mon{qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOW DEC)} = '01'..'12';
    s/(\d\d)-(\w{3})-(\d{4})/$3$mon{$2}$1/ for $DBDate,$sysdate;
    print qw(equal greater less)[$sysdate cmp $DBDate];

    Accepted Solution

    Hi, you could use:

    And parse both dates, in order to compare them.

    The library make it quite easy

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now