Android Question Verify a valid date

Marco Maria Vilucchi

Active Member
Licensed User
Longtime User
Hi all,
i'm trying to verify if a date is a real correct date.
31/04/2018 isn't a valid date
29/02/2018 isn't a valid date.
30/04/2018 is a valid date

DateTime.DateParse control only the format of the date (DateTime.DateParse("31/04/2018") is correct)

ValidDate on StringUtils library give a syntax error (not declared)

How can I veritay if my date i a correct real date?
Thanks
Marco
 

afields

Member
Licensed User
Helo!
one way to do that is to see if each of the date pieces are correct. ie see if the year's on the limit you want, then see if the month's number is ( of course) between 1 and 12, then ( if you declare and fill a litle array of 12 elements with the max number of each month) with the number of the month you'll have the item number of the array ( in your exemple say 4). That element have 30 ( wich is the max number of day in April). Finally if you have a febuary date you'll need to see if in that year there are another day. you can do this by dividing your year by 4 and obtaning the remaining. it will indicate if the febuary date is correct!
Better than that is to create a little function to handle what i've wrote and that return a 0 ( invalid date) or 1 ( valid date). so later you'll need only something like:
if valid_date("2014-02-29")= 1 then ...
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Upvote 0

netsistemas

Active Member
Licensed User
Longtime User
and for verify a time:

B4X:
Sub IsTimeHHmm(Time As String) As Boolean
    Try


        Dim PreviousFormat As String
        PreviousFormat = DateTime.TimeFormat
       
        DateTime.TimeFormat= "HH:mm"
         DateTime.TimeParse(Time)
       
        DateTime.TimeFormat= PreviousFormat
        Return True
    Catch
        Log(LastException)
        Return False
    End Try
   
End Sub
 
Upvote 0
Top