I need to do the time difference as $date2-$date1

I'll assume you need the difference in seconds. If you need minutes or hours, divide by 60 or by 60/60.

I've used Date::Calc for a long time with fine results. There are several functions (as well as an OOP interface for the same functions) available, depending on what you need. You'll have to do a little parsing, but then you turn the string into the seconds since the epoch (doesn't matter which one) and just subtract one from the other.

use strict; use warnings; use Date::Calc qw(Mktime); # Turns a date string into seconds since ep +och my $dt_str2="31/12/2009 6:29:26 p.m."; my $dt_str1="31/12/2009 6:14:25 p.m."; my ($day, $mo, $yr, $hr, $min, $sec, $mer) = $dt_str1 =~ m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) ([ap])}; $hr += 12 if $mer eq 'p'; die "Problem with date string $_" if $hr > 23; # Note element order for Date::Calc functions! my $dt1 = Mktime($yr, $mo, $day, $hr, $min, $sec); ($day, $mo, $yr, $hr, $min, $sec, $mer) = $dt_str2 =~ m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) ([ap])}; $hr += 12 if $mer eq 'p'; die "Problem with date string $_" if $hr > 23; my $dt2 = Mktime($yr, $mo, $day, $hr, $min, $sec); print $dt2 - $dt1, "\n";

Or you could make it into a loop if you had a lot of these to parse at once (and it's cleaner looking too).

use strict; use warnings; use Date::Calc qw(Mktime); # Turns a date string into seconds since ep +och my $dt_str2="31/12/2009 6:29:26 p.m."; my $dt_str1="31/12/2009 6:14:25 p.m."; my @dates_to_compare; for ($dt_str1, $dt_str2) { my ($day, $mo, $yr, $hr, $min, $sec, $mer) = m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) ([ap])}; $hr += 12 if $mer eq 'p'; die "Problem with date string $_" if $hr > 23; # Note element order for Date::Calc functi +ons! push @dates_to_compare, Mktime($yr, $mo, $day, $hr, $min, $sec); } # using pop clears out the array, which is helpful when looping print pop(@dates_to_compare) - pop(@dates_to_compare), "\n";

I hope this helps

marmot

In reply to Re: More Date Arithmetic/Manipulation: Question by furry_marmot
in thread More Date Arithmetic/Manipulation: Question by newbie01.perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.