Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How do I tell the time between two dates?

by kevbot (Vicar)
on Apr 16, 2016 at 05:04 UTC ( [id://1160603]=note: print w/replies, xml ) Need Help??


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

Hello htmanning,

The link that Your Mother provided mentions Time::Piece. Here's one way to solve your problem using it.

#!/usr/bin/env perl use strict; use warnings; use Time::Piece; 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; my $elapsed_days = $elapsed_seconds/86400; print " Hire Date: $hiredate\n"; print " Now Date: $nowdate\n"; print " Elapsed Days: $elapsed_days\n"; exit;
Here's a good article describing how to use Time::Piece: http://perltricks.com/article/59/2014/1/10/Solve-almost-any-datetime-need-with-Time--Piece/

Replies are listed 'Best First'.
Re^2: How do I tell the time between two dates?
by htmanning (Friar) on Apr 16, 2016 at 05:26 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1160603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-23 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found