Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl Sensitive Sunglasses
 
PerlMonks

How do I find the difference in days between two dates, in a cool perl way?

 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

( #17057=categorized question: print w/ replies, xml ) Need Help??
Contributed by frankus on Jun 08, 2000 at 15:53 UTC
Q&A  > dates and times


Answer: How do I find the difference in days between two dates, in a cool perl way?
contributed by reptile

If the dates are in epoch seconds, take the difference and divide it by the number of seconds in a day (which is 86400). Like so:

my $days_difference = int(($time1 - $time2) / 86400);
The int() there, by the way, chops off the decimal so you don't end up with something like 4.3231235. If you're curious, the remainder of that division is the number of seconds left after finding the number of days, so if you want "x days, x hours", you can do this next:
my $hours_left = int((($time1 - $time2) % 86400) / 3600);
That should give you an idea. If your dates aren't in epoch seconds, you'll either have to get them there somehow, or check out one of the Date::* modules.
Answer: How do I find the difference in days between two dates, in a cool perl way?
contributed by mojotoad

Check out Time::Piece. This overides localtime(), gmtime(), arithmetic operations, and stringification so that date calculations are remarkably simple. For example:

use Time::Piece; $before = Time::Piece->strptime("2001/01/01", "%Y/%m/%d"); $now = localtime; $diff = $now - $before; print int($diff->days), " days since $before\n";
Answer: How do I find the difference in days between two dates, in a cool perl way?
contributed by phenom

Using Date::Calc:

#!/usr/bin/perl use strict; use warnings; use Date::Calc qw/Delta_Days/; my @first = (2001, 9, 12); my @second = (2004, 3, 11); my $dd = Delta_Days( @first, @second ); print "Difference in days: $dd\n";
Answer: How do I find the difference in days between two dates, in a cool perl way?
contributed by autarch

Using DateTime (assuming $dt1 and $dt2 are DateTime objects):

my $duration = $dt1->delta_days($dt2); print $duration->days;

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
  • 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.
  • Login:
    Password
    remember me
    What's my password?
    Create A New User

    Community Ads
    Chatterbox
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users
    Others examining the Monastery: (13)
    ikegami
    GrandFather
    jdporter
    atcroft
    herveus
    thezip
    Eyck
    MonkE
    ssandv
    vitoco
    gnosti
    wanradt
    im2
    As of 2009-11-21 04:20 GMT
    Sections
    The Monastery Gates
    Seekers of Perl Wisdom
    Meditations
    PerlMonks Discussion
    Categorized Q&A
    Tutorials
    Obfuscated Code
    Perl Poetry
    Cool Uses for Perl
    Perl News
    Information
    PerlMonks FAQ
    Guide to the Monastery
    What's New at PerlMonks
    Voting/Experience System
    Tutorials
    Reviews
    Library
    Perl FAQs
    Other Info Sources
    Find Nodes
    Nodes You Wrote
    Super Search
    List Nodes By Users
    Newest Nodes
    Recently Active Threads
    Selected Best Nodes
    Best Nodes
    Worst Nodes
    Saints in our Book
    Leftovers
    The St. Larry Wall Shrine
    Offering Plate
    Awards
    Craft
    Snippets Section
    Code Catacombs
    Quests
    Editor Requests
    Buy PerlMonks Gear
    PerlMonks Merchandise
    Planet Perl
    Perlsphere
    Use Perl
    Perl.com
    Perl 5 Wiki
    Perl Jobs
    Perl Mongers
    Perl Directory
    Perl documentation
    CPAN
    Random Node
    Voting Booth

    Future historians will find that the material characteristic of the current era is...

    Aluminium
    Plastic
    Oil
    Water
    Carbon dioxide
    Copper
    Iron
    Silicon
    Salt
    Uranium
    Hydrogen
    Other

    Results (726 votes), past polls