Prev: why is child process a negative number?
Next: FAQ 4.68 Why does passing a subroutine an undefined element in a hash create it?
From: Hugh on 23 Apr 2010 01:12 John W. Krahn wrote: >Hugh(a)earthlink.net wrote: >> Perl neophyte here. >> >> I have a record date in the format mm/dd/yyyy and I want to compare it to >> another date in the same format. Is there a simple way to accomplish this >> task? All I want to do is say 'if date1 <= date0' do or don't do >> something. I've looked, but to no avail. Help please... > >if ( join( '', ( split /\//, $date1 )[ 2, 0, 1 ] ) le join( '', ( split >/\//, $date0 )[ 2, 0, 1 ] ) ) { > do something; > } > Perfect! Thanks, Hugh
From: Hugh on 23 Apr 2010 01:16
Tad McClellan wrote: >Hugh(a)earthlink.net <Hugh(a)earthlink.net> wrote: > >> I have a record date in the format mm/dd/yyyy > > >Too bad it isn't in a more sensible format, such as yyyy/mm/dd... > > >> and I want to compare it to >> another date in the same format. Is there a simple way to accomplish this >> task? > > >If they were in yyyy/mm/dd, then > > $date1 le $date0 > >would be all you need. > >But since you seem to be restricted to using a silly date format, >the Date::Manip module can help. > > >--------------------------- >#!/usr/bin/perl >use warnings; >use strict; >use Date::Manip; > >my $date0 = '04/01/2010'; >my $date1 = '04/02/2010'; > >if ( Date_Cmp(ParseDate($date0), ParseDate($date1)) <= 0 ) { > print "$date0 is less than or equal to $date1\n"; >} >else { > print "$date0 is greater than $date1\n"; >} > >($date0, $date1) = ($date1, $date0); # swap values, and try it again > >if ( Date_Cmp(ParseDate($date0), ParseDate($date1)) <= 0 ) { > print "$date0 is less than or equal to $date1\n"; >} >else { > print "$date0 is greater than $date1\n"; >} >--------------------------- Thanks. I think John Krahn's is a bit more elegant. :-) Hugh |