Date and Time Validation

Started by Rolf Brandt, August 06, 2010, 07:22:35 AM

Previous topic - Next topic

Rolf Brandt

Based on Paul's example http://www.planetsquires.com/protect/forum/index.php?topic=2617.0 I have extended the function for my personal use a little. It can now vaidate date, time or a combination of the two. It also accepts the date in different formats (could be further extended to local date formats).


'--------------------------------------------------------------------------------
' IsValidDateTime
' Validate Date, Time, or a combination of both
' Valid date format: YYYYMMDD, YYYY-MM-DD, YYYY/MM/DD
' Valid time format: HH:mm:SS, HH:mm
'--------------------------------------------------------------------------------
Function IsValidDateTime(sDate As String) As Long
   Local tSystemTime As SYSTEMTIME
   Local tFileTime   As FILETIME

   Local dPart       As String
   Local tPart       As String
   
   Replace "-" With "" In sDate
   Replace "/" With "" In sDate
   
   'Split date and time parts
   If Instr(sDate," ") Then   'date and time
      dpart = Left$(sDate, Instr(sDate," ") - 1)
      tPart = Trim$(Mid$(sDate,Instr(sDate," ")))
   Else
      If Tally(sDate,":") Then   'Time
         tPart = sDate
      Else
         dPart = sDate
      End If
   End If
   
   If Len(dPart) = 0 Then
      dPart = Right$(Date$, 4) & Left$(Date$, 2) & Mid$(Date$, 4, 2)     
   End If
   sDate = dPart & " " & tPart

   tSystemTime.wYear    = Val(Left$(sDate, 4))
   tSystemTime.wMonth   = Val(Mid$(sDate, 5, 2))
   tSystemTime.wDay     = Val(Mid$(sDate, 7, 2))
   tSystemTime.wHour    = Val(Mid$(sDate, 10, 2))
   tSystemTime.wMinute  = Val(Mid$(sDate, 13, 2))
   tSystemTime.wSecond  = Val(Mid$(sDate, 16, 2))
   
   Function = SystemTimeToFileTime( tSystemTime, tFileTime )
End Function

Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Paul Squires

Excellent!

I think that I will add this to the official list of FireFly Functions.
Paul Squires
PlanetSquires Software

Rolf Brandt

Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)