Compare Dates
am 31.08.2007 18:24:36 von amerar
Hi All,
Here is a good one: I need to compare 2 dates, without the year.
Basically I want to know if a specific calendar date falls between two
other dates. Everything is in YYYY-MO-DD, the way Perl wants it.
So, say I am looking for a birthday. I do not care about the year, I
just want to know if a birthday falls between two dates in the
calendar.......
Any suggestions? Date::Manip??? Did not seem to do what I want.
Thanks!
Re: Compare Dates
am 31.08.2007 18:51:59 von Gunnar Hjalmarsson
amerar@iwc.net wrote:
> Here is a good one: I need to compare 2 dates, without the year.
> Basically I want to know if a specific calendar date falls between two
> other dates. Everything is in YYYY-MO-DD, the way Perl wants it.
Just strip the year:
my $day = substr $date, 5;
and do stringwise comparisons.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: Compare Dates
am 01.09.2007 00:02:11 von Petr Vileta
amerar@iwc.net wrote:
> Hi All,
>
> Here is a good one: I need to compare 2 dates, without the year.
> Basically I want to know if a specific calendar date falls between two
> other dates. Everything is in YYYY-MO-DD, the way Perl wants it.
>
> So, say I am looking for a birthday. I do not care about the year, I
> just want to know if a birthday falls between two dates in the
> calendar.......
>
$birthdate = '1978-11-02';
$birthday = '11-02';
if(substr($birthdate,5) eq $birthday) {print "Bingo";}
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Re: Compare Dates
am 01.09.2007 02:39:45 von benkasminbullock
On Fri, 31 Aug 2007 09:24:36 -0700, amerar@iwc.net wrote:
> Here is a good one: I need to compare 2 dates, without the year.
> Basically I want to know if a specific calendar date falls between two
> other dates. Everything is in YYYY-MO-DD, the way Perl wants it.
>
> So, say I am looking for a birthday. I do not care about the year, I
> just want to know if a birthday falls between two dates in the
> calendar.......
>
> Any suggestions? Date::Manip??? Did not seem to do what I want.
Change the year of all your dates to be the same arbitrary year (make it a
leap year), then use the Day_of_Year function from
Date::Calc to get the day of the year (see
http://www.unix.org.ua/orelly/perl/cookbook/ch03_07.htm), then compare the
numbers.
Re: Compare Dates
am 01.09.2007 15:56:05 von Big and Blue
Ben Bullock wrote:
>
> Change the year of all your dates to be the same arbitrary year (make it a
> leap year), then use the Day_of_Year function from
> Date::Calc to get the day of the year (see
> http://www.unix.org.ua/orelly/perl/cookbook/ch03_07.htm), then compare the
> numbers.
Or just use the month and day while assuming all months have 32 days.
sub date_index {
(my ($m, $d)) = $_[0] =~ /^\d{4}-(\d{2})-(\d{2})$/;
return ($m<<5) + $d;
}
then just compare the results from date_index calls.
--
Just because I've written it doesn't mean that
either you or I have to believe it.