CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
date comparison by lt and gt

 



regex2012
User

Dec 4, 2015, 7:17 AM

Post #1 of 7 (1713 views)
date comparison by lt and gt Can't Post

What does this actually do?

Code
$date=12/04/2015 
$old_date=12/01/2015
if ($date lt $old_date) {
print "yes";
}

Will it compare dates? Or is this not possible by the means of lt and gt?



BillKSmith
Veteran

Dec 4, 2015, 7:52 AM

Post #2 of 7 (1711 views)
Re: [regex2012] date comparison by lt and gt [In reply to] Can't Post

Perl treats both your variables as strings. The operators compare your strings in 'dictionary' order. This really does not seem to make any sense for dates formatted as you have them. A common idiom is to store dates as yyyymmdd. You still cannot do arithmetic on them, but you can compare and sort them with perl's built-in comparison operators.

Note: Your example is a special case (Only one field is different between the two dates) where your code does work correctly.
Good Luck,
Bill


bulrush
User

Dec 5, 2015, 10:10 AM

Post #3 of 7 (1666 views)
Re: [regex2012] date comparison by lt and gt [In reply to] Can't Post

That' won't work. Perl doesn't have a date datatype like SQL. Here's how I compare dates. I first convert them to YYYYMMDD format. YYYY-MM-DD format also works.


Code
$d1='20151201'; 
$d2='20151204';
if ($d1<$d2)
{print "d1 is less than d2\n";
}

-----


(This post was edited by bulrush on Dec 5, 2015, 10:10 AM)


Laurent_R
Veteran / Moderator

Dec 6, 2015, 9:31 AM

Post #4 of 7 (1659 views)
Re: [bulrush] date comparison by lt and gt [In reply to] Can't Post

Your example is entirely correct, but if you use the YYYY-MM-DD format (which you suggested as an alternative), then you should use the lt string comparison operator rather than the < numerical comparison operator.


bulrush
User

Dec 7, 2015, 3:51 AM

Post #5 of 7 (1634 views)
Re: [Laurent_R] date comparison by lt and gt [In reply to] Can't Post

Laurent is right, the dashes turn the date from a number to a string.
-----


FishMonger
Veteran / Moderator

Dec 7, 2015, 8:29 AM

Post #6 of 7 (1631 views)
Re: [regex2012] date comparison by lt and gt [In reply to] Can't Post

The code you posted won't compile due to the missing semicolons on the assignment statements. Assuming that was simply a typo when posting, the next problem is that the "dates" aren't actually date strings due to the missing quotes. They are mathematical equations (12 divided by 4 divided by 2015) which is why they resulted in floats.

Bulrush's solution assigns the dates as strings but uses a numerical operator when comparing them, which works in this case/context but not all.


regex2012
User

Dec 7, 2015, 1:37 PM

Post #7 of 7 (1622 views)
Re: [FishMonger] date comparison by lt and gt [In reply to] Can't Post

Thanks for all your help! I managed to get this running by breaking the date down into $month\/$day\/$year and comparing the numerical values there.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives