"be consistent" PerlMonks

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

by frankus (Priest)
 on Jun 08, 2000 at 15:53 UTC 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); [download]``` 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); [download]``` 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"; [download]``` 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; [download]``` 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"; [download]``` Answer: How do I find the difference in days between two dates, in a cool perl way?contributed by talexb Use the utc_rd_values method in DateTime to get the day number of the date you're looking at: ```my @start = \$dt1->utc_rd_values; my @finish = \$dt2->utc_rd_values; my \$delta = \$finish[0] - \$start[0]; [download]``` Now \$delta contains the number of days between the two dates. Answer: How do I find the difference in days between two dates, in a cool perl way?contributed by shmem ```perl -MDate::Parse -le 'print+(str2time(pop) - str2time(pop)) / 86400' + 2016-02-28 2016-03-01 2 [download]``` Note, however, that it seems not to work for dates before January 1st 1967. At least, not with Date::Parse version 2.30, which is the version I have. Update: this seems to be a long-known bug: rt://105031; rt://84075 gives a little more info as well.

• 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
 [Discipulus]: nice Perl dreams, folks.. shower->Dumas->bed [erix]: pere ou fils? [Lady_Aleena]: Discipulus, good night. [Discipulus]: pere, le vicomte de bragelonne.. wonderfull [erix]: I'll keep that in mind (haven't read), buona notte :) [Discipulus]: oh erix but is the fourth of cycle;read them in order [erix]: hm, perhaps I can just read it here Discipulus zzz...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-05-23 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (182 votes). Check out past polls.