in reply to Re: Comparing Dates
in thread Comparing Dates

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: Comparing Dates
by ikegami (Patriarch) on Mar 13, 2006 at 19:48 UTC
    When dealing with date/times, you must usually convert to epoch time first.
    use Time::Local qw( timelocal ); sub get_time_from_local_time_string { my ($time_string) = @_; my ($M, $D, $Y, $h, $m, $s, $ampm) = $time_string =~ m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) (\w+)}; if ($h == 12) { $h -= 12 if lc($ampm) eq 'am'; } else { $h += 12 if lc($ampm) eq 'pm'; } return timelocal($s, $m, $h, $D, $M-1, $Y); } my $time1 = get_time_from_local_time_string($time_string_1); my $time2 = get_time_from_local_time_string($time_string_2); if (abs($time1 - $time2) > 72*60*60) { ... }

    References:
    Time::Local
    localtime

    Update: Application of AM/PM was incorrect. Fixed.

      Thanks... I tried it but with $D+1 and $M... Because it is complaining abot month and days not in range... But some how the time diff is showing 0 even after i put different dates... I could see from the log the dates but for both the dates the retuen value is showing up the same??? Any thoughts....

        I tested my code a bit before posting it. For example,
        '03/10/2006 4:39:11 PM' compared to '03/13/2006 4:39:11 PM' was false, while
        '03/10/2006 4:39:11 PM' compared to '03/13/2006 4:39:12 PM' was true.

        Can you give me the exact date strings that are resulting in problems? It sounds like the regexp isn't matching (so all the variables are undefined and treated as 0s).

        Below is my code with a a die statement added to confirm the above:

        sub get_time_from_local_time_string { my ($time_string) = @_; my ($M, $D, $Y, $h, $m, $s, $ampm) = $time_string =~ m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) (\w+)} or die("Bad input"); if ($h == 12) { $h -= 12 if lc($ampm) eq 'am'; } else { $h += 12 if lc($ampm) eq 'pm'; } return timelocal($s, $m, $h, $D, $M-1, $Y); }