Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Comparing Dates

by Miguel (Friar)
on Mar 14, 2006 at 01:46 UTC ( #536438=note: print w/ replies, xml ) Need Help??


in reply to Comparing Dates

This might do the work:

#!/usr/bin/perl -w use strict; print CompareDates( { DATE_1 => "01/10/2006 4:39:11 PM", DATE_2 => "02/10/2006 4:39:11 PM", LIMIT => 72, # Hours PATTERN => '%d/%m/%Y %l:%M:%S %P' # Optional } ); print CompareDates( { DATE_1 => "01/10/2006 4:39:11 PM", DATE_2 => "04/10/2006 4:39:11 PM", LIMIT => 72, # Hours } ); print CompareDates( { DATE_1 => "01/10/2006 4:39:11 PM", DATE_2 => "06/10/2006 4:39:11 PM", LIMIT => 72, # Hours } ); sub CompareDates { my $params = shift; use DateTime::Format::Strptime; my $parser = DateTime::Format::Strptime->new( pattern => $params->{PATTERN} || '%d/%m/%Y %l:%M:%S %P' ); my $dt1 = $parser->parse_datetime($params->{DATE_1}); my $dt2 = $parser->parse_datetime($params->{DATE_2}); my $dtchk = $dt2->subtract(hours=>$params->{LIMIT}); my $pref = "The difference between '$params->{DATE_1}' AND '$params->{DATE_2}' is"; my $suf = "$params->{LIMIT} hours.\n"; return "$pref bigger than $suf" if ($dtchk > $dt1); return "$pref equal to $suf" if ($dtchk == $dt1); return "$pref smaller than $suf" if ($dtchk < $dt1); }
Prints:
The difference between '01/10/2006 4:39:11 PM' AND '02/10/2006 4:39:11 PM' is smaller than 72 hours. The difference between '01/10/2006 4:39:11 PM' AND '04/10/2006 4:39:11 PM' is equal to 72 hours. The difference between '01/10/2006 4:39:11 PM' AND '06/10/2006 4:39:11 PM' is bigger than 72 hours.


Comment on Re: Comparing Dates
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-10-31 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls