How compare two dates or convert date to number

  • Follow


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...

Thank you in advance,

Hugh
0
Reply Hugh 4/23/2010 2:02:41 AM

Hugh@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;
     }



John
-- 
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway
0
Reply John 4/23/2010 2:51:31 AM


Hugh@earthlink.net wrote:
>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...

Date::Calc is your friend.

Of course, had you used an ISO-8601 compliant format then a simple
alphanumerical comparision using 'le' would have been sufficient.

jue
0
Reply J 4/23/2010 3:01:18 AM

Hugh@earthlink.net <Hugh@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";
}
---------------------------


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
0
Reply Tad 4/23/2010 3:51:20 AM

John W. Krahn wrote:

>Hugh@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
0
Reply Hugh 4/23/2010 5:12:57 AM

J�rgen Exner wrote:

>Hugh@earthlink.net wrote:
>>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...
>
>Date::Calc is your friend.
>
>Of course, had you used an ISO-8601 compliant format then a simple
>alphanumerical comparision using 'le' would have been sufficient.
>
Thanks.

Hugh
0
Reply Hugh 4/23/2010 5:13:36 AM

Tad McClellan wrote:

>Hugh@earthlink.net <Hugh@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
0
Reply Hugh 4/23/2010 5:16:12 AM

6 Replies
538 Views

(page loaded in 0.609 seconds)

Similiar Articles:













7/24/2012 9:06:53 PM


Reply: