htmanning has asked for the wisdom of the Perl Monks concerning the following question:

I'm going in circles trying to re-invent the wheel and there must be an easier way. I need to tell the time that has elapsed between two dates.
$hiredate = "2014-05-05"; $nowdate = "2016-04-15";
I've tried to spilt the numbers up and do some math, but it doesn't work. I'm ending up with negative numbers if the months are the same, etc., etc. This was my lame attempt. Can someone give me a better way?
@words = split (/-/, $hiredate); $hire_yr = "@words[0]"; $hire_mo = "@words[1]"; $hire_day = "@words[2]"; @words_now = split (/-/, $ymd); $now_yr = "@words_now[0]"; $now_mo = "@words_now[1]"; $now_day = "@words_now[2]"; $since_yr = $now_yr - $hire_yr; $since_mo = $now_mo - $hire_mo; $since_day = $now_day - $hire_day; unless ($since_mo == abs($since_mo)) { $since_yr = $since_yr -1; }

Replies are listed 'Best First'.
Re: How do I tell the time between two dates?
by Your Mother (Archbishop) on Apr 16, 2016 at 04:46 UTC
Re: How do I tell the time between two dates?
by kevbot (Vicar) on Apr 16, 2016 at 05:04 UTC
      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.
        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

        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.
Re: How do I tell the time between two dates?
by Marshall (Canon) on Apr 16, 2016 at 04:53 UTC
    There are many modules and Perl functions that deal with dates. In the easiest case, each date is converted to a number of "epoch" seconds, since Jan 1, 1970 for Unix. Life is easy if both dates are after that date and both dates are calculated in UTC (basically GMT) which is in the same time zone. There are ways to deal with other dates.

    Look at: date time perhaps or use the Perl time functions Getting started with DateTime Many variants of these functions are available. Also see http://perldoc.perl.org/Time/Local.html

Re: How do I tell the time between two dates?
by hippo (Archbishop) on Apr 16, 2016 at 07:52 UTC
    I'm going in circles trying to re-invent the wheel and there must be an easier way.

    Handy hint: every time you are in that situation, head for the FAQ.

Re: How do I tell the time between two dates?
by Anonymous Monk on Apr 16, 2016 at 06:37 UTC