Date Comparison Problems (was Date)

Rob Cozens rcozens at pon.net
Mon Sep 11 11:06:49 EDT 2006


Ian,

>The humourous
>aspect, for me, in all this, is that after writing many lines of code
>to achieve a valid date it seems all I needed to do was code
>
>if theDOB is a date.  Isn't Revolution wonderful!

Not all that wonderful in this case... because Rev will accept any 
number as a valid date--in seconds.

Try "put 123456 is a date"; you'll find "true" in the message box.

I'm afraid date validation does take many lines of code.  The 
following validates a string based on the computer's date format, as 
defined in system preferences or control panel settings:

function validDate theString -- 5 April 04:RCC
   put stripBlanks(theString,false) into theString -- strips leading spaces
   get systemDateFormat() -- see below
   set the itemDelimiter to char -1 of it -- theSeparator
   if the number of items of theString <> 3 then return false
   delete the last char of it
   delete char offset("mm",it) of it -- remove double characters, if any
   delete char offset("dd",it) of it
   delete char offset("yy",it) of it
   repeat with x = 1 to 3
     switch char x of it
     case "m"
       put item x of theString into theMonth
       if not validDigits(theMonth) then return false -- validDigits 
restricts chars to "0" through "9" ("is a number" allows decimal and 
minus sign)
       if theMonth < 1 or theMonth > 12 then return false
       break
     case "d"
       put item x of theString into theDay
       if not validDigits(theDay) then return false
       if theDay < 1 or theDay > 31 then return false
       break
     case "y"
       put item x of theString into theYear
       if not validDigits(theYear) then return false
       put length(theYear) into charCount
       if charCount <> 2 and charCount <> 4 then return false
       if charCount is 2 then add 2000 to theYear -- note 
centuryCuttoff is ignored (all two digit years are set to the current 
century)
       if theYear < 1 then return false
       break
     end switch
   end repeat
   if theDay < 29 then return 
true&return&theYear&comma&theMonth&comma&theDay&",0,0,0,0"
   if theMonth is 2 then
     if theDay > 29 or not leapYear(theYear) then return false -- see below
     else return true&return&theYear&comma&theMonth&comma&theDay&",0,0,0,0"
   else
     if offset("0",theMonth) = 1 then delete char 1 of theMonth
     set the itemDelimiter to comma
     if theMonth is among the items of "1,3,5,7,8,10,12" then return 
true&return&theYear&comma&theMonth&comma&theDay&",0,0,0,0"
     if theDay is 31 then return false
     else return true&return&theYear&comma&theMonth&comma&theDay&",0,0,0,0"
   end if
end validDate

function leapYear theYear -- 16 Mar 04:RCC
   if theYear mod 100 is 0 then return (theYear mod 400 = 0)
   else return (theYear mod 4 = 0)
end leapYear

-- 

Rob Cozens
CCW, Serendipity Software Company

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)


More information about the use-livecode mailing list