Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

How do i compare dates with format

by cab (Beadle)
on Jan 18, 2002 at 13:11 UTC ( #139774=categorized question: print w/replies, xml ) Need Help??
Contributed by cab on Jan 18, 2002 at 13:11 UTC
Q&A  > dates and times

Answer: How do i compare dates with format
contributed by metadoktor

Use Date::Calc from CPAN. Specifically, you'd want to look at Delta_Days or Delta_YMD; or you could use Mktime to convert both dates to seconds and then compare the seconds.

Answer: How do i compare dates with format
contributed by fuzzysteve

It depends on how you want to compare them.

If you just want to check which is later, reverse the order of the parts, i.e. (Make sure you keep all fields fixed width, i.e. zero padded if necessary.)

Then simply compare them as strings! However, you are likely to run into the "Y2K" problem, since your years are only two digits.

Answer: How do i compare dates with format
contributed by cab

Time::Local, which provides the inverse of localtime.

Answer: How do i compare dates with format
contributed by poolpi

Using DateTime and DateTime::Format::Duration:

#!/usr/bin/perl use strict; use warnings; use DateTime; use DateTime::Format::Duration; my @dt; for (qw( 30.01.08 05.03.08 )) { local @_ = split /\./; push @dt, DateTime->new( year => $_[2] + 2000, month => $_[1], day => $_[0], time_zone => "Europe/Paris" ); } my $dur = $dt[1]->subtract_datetime( $dt[0] ); $dur->is_negative and warn $dt[1]->dmy . 'is older than ' . $dt[0]->dmy . "\n"; # Choose the pattern to feet your needs my $dtfd = DateTime::Format::Duration->new( pattern => '%V weeks, %e days' ); my $formated_dtd = $dtfd->format_duration($dur); print <<QUOTE; Difference in weeks and days, between @{[$dt[1]->dmy]} and @{[$dt[0]->dmy]}: $formated_dtd. QUOTE
Date::Simple parses date strings and creates numerically comparable objects...
# Difference in days between two dates: $diff = date('2001-08-27') - date('1977-10-05');

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (4)
    As of 2018-08-16 15:34 GMT
    Find Nodes?
      Voting Booth?
      Asked to put a square peg in a round hole, I would:

      Results (168 votes). Check out past polls.