Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How do i compare dates with format dd.mm.yy

( #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 dd.mm.yy
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 dd.mm.yy
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. yy.mm.dd. (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 dd.mm.yy
contributed by cab

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

Answer: How do i compare dates with format dd.mm.yy
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2014-12-21 00:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (100 votes), past polls