Date validation

Thank you, Geoff

@Mike_Duglas sent me code below and it looks clean and easy to extend, also no 3rd-party

IsDate               PROCEDURE  (pInput)                   ! Declare Procedure
fmt                  STRING(10),DIM(5)                     ! 5 mostly used date formats
dtValidStart         LONG,AUTO                             ! lowest valid date
dtValidEnd           LONG,AUTO                             ! highest valid date
dtTest               LONG,AUTO                             ! deformatted value
i                    LONG,AUTO                             ! 

  CODE
  fmt[1] = '@d17'
  fmt[2] = '@d05'
  fmt[3] = '@d05.'
  fmt[4] = '@d06'
  fmt[5] = '@d06.'
  dtValidStart = DATE(1, 1, 1801)
  dtValidEnd   = DATE(1, 1, 3030)
  !- loop thru each date format
  LOOP i=1 TO MAXIMUM(fmt, 1)
    !- try to deformat
    dtTest = DEFORMAT(pInput, fmt[i])
    
    IF (dtTest >= dtValidStart AND dtTest <= dtValidEnd) AND (pInput=FORMAT(dtTest, fmt[i]))
      !- the date in date range and formatted date=input
      !- valid date
      RETURN TRUE
    END
  END
  RETURN FALSE
1 Like

Hi Guennadi - yes that looks like a good (and much simpler) way to do it rather than do it “long hand”.

cheers

Geoff R

1 Like

just thinking about this a bit more Guennadi you might need to change Mike’s approach if you want to allow @d6 dates like 1/1/2020 or 29/2/2020 and so on as they will return false not true as they are not using two digits for day and month (as opposed to 01/01/2020 and 29/02/2020). Today is 3rd of July and most people here in Australia would write that as 3/7/2020 or 3/7/20 (or if in the USA then 7/3/2020 or 7/3/20).

Anyway worth making a few tests.

I think you need just add more date format to validate

fmt[6] = '@d6'

and change dim
fmt STRING(10),DIM(6)

easy to try it but I think you will find, for example, 30/1/2020 with @d6 will be formatted as 30/01/2020 and therefore it will return false (not a valid date) when you want it to say true. This would make it fail for the first 9 months of the year. And similar problems with the first nine days in each month except I think the leading zero comes back as a space. You could easily fix that by doing left() on the format on equivalence test but the month is less easy to fix.

also there is the picture end user select code . i can send post it here. I dont know if its public domain but the author can be contacted.