in reply to Re: How do I tell the time between two dates?
in thread How do I tell the time between two dates?

Thanks so much for this. What I'm trying to do is present the number like this: It has been 2 years, 3 months, and 4 days since you were hired. I don't know how to convert the number of days to that. Thanks.
  • Comment on Re^2: How do I tell the time between two dates?

Replies are listed 'Best First'.
Re^3: How do I tell the time between two dates?
by Your Mother (Archbishop) on Apr 16, 2016 at 05:56 UTC
    use strictures; use DateTime; use DateTime::Format::Human::Duration; my $hire = DateTime->new( year => 1605, month => 11, day => 5 ); my $now = DateTime->new( year => 1606, month => 1, day => 31 ); my $span = DateTime::Format::Human::Duration->new(); my $dur = $now - $hire; print $span->format_duration($dur), $/;
    2 months, 3 weeks, and 5 days

    DateTime & DateTime::Format::Human::Duration & :P :P :P

Re^3: How do I tell the time between two dates?
by kevbot (Vicar) on Apr 16, 2016 at 06:04 UTC
    The Time::Duration module has the functionality you need. For example,
    #!/usr/bin/env perl use strict; use warnings; use Time::Piece; use Time::Duration; my $date_format = '%Y-%m-%d'; my $hiredate = Time::Piece->strptime('2014-05-05', $date_format ); my $nowdate = Time::Piece->strptime('2016-04-15', $date_format ); my $elapsed_seconds = $nowdate - $hiredate; print " Hire Date: $hiredate\n"; print " Now Date: $nowdate\n"; print "It has been ", duration($elapsed_seconds), " since you were hir +ed.\n"; exit;
    See Is there a CPAN module for converting seconds to English? for a helpful discussion on this topic.