Days between two dates

Simon Wistow simon at thegestalt.org
Thu Dec 22 15:28:48 GMT 2005


On Thu, Dec 22, 2005 at 03:10:24PM +0000, Paul Makepeace said:
> A should-be-obvious question. How does one find the number of days
> between two dates, without converting to epoch? 

This is one of those things that really bothers me about DateTime but 
which is aprtly because this has no definite answer

	http://datetime.perl.org/index.cgi?MathProblems

Dates and Times suck.

Off the top of my head, one way of doing it is

--- >8 ---

my $start = DateTime->new( year => 2005, month => 12, day => 14 );
my $end   = DateTime->new( year => 2005, month => 12, day => 25 );
my $span  = DateTime::Span->from_datetimes( start => $start, end => $end 
);


my $set   = DateTime::Set->from_recurrence(
             recurrence => sub { 
	   	 	                     $_[0]->truncate( to => 'day' )
                                      ->add( days => 1) 
                               },
               span => $span,
             );

my $i = 1;
while ($set->next) {
    print $i++."\n";
}



--- >8 ---


I'm hoping there's a better way.




More information about the london.pm mailing list