Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

get remaining hours

by doubledecker (Scribe)
on May 06, 2013 at 04:29 UTC ( #1032169=perlquestion: print w/replies, xml ) Need Help??
doubledecker has asked for the wisdom of the Perl Monks concerning the following question:


I'm trying to find out remaining difference of the day in hours, but not sure of how to proceed further. Here is what I've tried so far...

#!/usr/bin/perl use strict; use DateTime; my $start = '2013/01/08 09:26:07'; my $end = '2013/04/11 20:19:12'; my @start_list = split /[ :\/]/, $start; my @end_list = split /[ :\/]/, $end; my %start; @start{qw[year month day hour minute second]} = @start_list; my %end; @end{qw[year month day hour minute second]} = @end_list; my $start = DateTime->new(%start); my $end = DateTime->new(%end);

I want to get remaining hours left for start date and total hours elapsed for end date. any help is appreciated

Replies are listed 'Best First'.
Re: get remaining hours
by Kenosis (Priest) on May 06, 2013 at 06:17 UTC

    Perhaps the following will be helpful:

    use strict; use warnings; use Date::Parse; my $start = '2013/01/08 09:26:07'; my $end = '2013/04/11 20:19:12'; print sprintf "%.2f", ( str2time($end) - str2time($start) ) / 3600;



    str2time parses a date/time string, returning a unix time value in seconds. It's used above to calculate the difference (delta) between your start and end dates/times. The output is the difference in hours.

Re: get remaining hours
by sundialsvc4 (Abbot) on May 06, 2013 at 13:48 UTC

    The CPAN library, at, contains thousands of packages which deals with dates, such as Date::Calc, Date::Manip, or Date::Simple.

    For example, click on the Date::Simple link and look at the EXAMPLES section:

    use Date::Simple (); my $date = Date::Simple->new('1972-01-17'); ...

    An opaque object as provided by this particular (pre-built, pre-tested) CPAN package, has among other things the ability to parse a date-string such as the one that you have, and to “do things with” the resulting date-value.   You do not have to be concerned with exactly how the magic works, because the logic has been rigorously tested (even as part of the process of installing the package on your system).   To you, that object “is a” date, and a very smart one at that.

    Although this particular package deals only with dates, not hours ... have a peek now at, say, Date::Calc::Object or Date::Calc.   :-)   Truly, there is nothing having to do with dates ... no, not even in the Mayan calendar (DateTime::Calendar::Mayan), if you are so inclined, whereby you must “roll your own.”

Re: get remaining hours
by Anonymous Monk on May 06, 2013 at 07:06 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032169]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-05-26 14:57 GMT
Find Nodes?
    Voting Booth?