Hello!
There is, for example, $period = "12.10.2010-15.10.2011" and there is $date = "14.03.2011"
So how can I check if $date in $period ?
Please do not suggest from the period to do an array, where to put all its intermediate dates.
Thank!
|
2 answers
use v5.10; use strict; use warnings; sub cmp_date { my ($d1, $m1, $y1) = split /\./, shift; my ($d2, $m2, $y2) = split /\./, shift; if ($y1 > $y2) { return 1; } elsif ($y1 < $y2) { return -1; } else { if ($m1 > $m2) { return 1; } elsif ($m1 < $m2) { return -1; } else { if ($d1 > $d2) { return 1; } elsif ($d1 < $d2) { return -1; } else { return 0; } } } } my $d = '14.03.2012'; my $p = "12.10.2010-15.10.2011"; my ($d1, $d2) = split /-/, $p; if (cmp_date($d, $d1) > 0 and cmp_date($d, $d2) < 0) { say "OK"; } else { say "NO"; } You can use POSIX::strptime or DateTime::Format::strptime , but it seemed to me that it was faster to write it yourself.
- Works! Thanks you!! ) - AIex
- @AIex, note here if the date coincides with one of the ends of the period will be
NO. If not, just replace>0with>=0- andy.37 - OK! And accordingly <0 on <= 0 - AIex
|
use strict; use warnings; sub ndate { join '', reverse split /\./, shift; } my $d = '14.03.2012'; my $p = "12.10.2010-15.10.2012"; my ($d1, $d2) = map {ndate $_} split /-/, $p; if( ndate($d) <= $d2 and ndate($d) >= $d1 ) { print "Yes"; } else { print "No"; } The ndate function turns a date from the format DD.MM.YYYY into the number YYYYMMDD. After that, the numbers can be compared in the usual way for more or less.
|