Is there an 'IsDate' function in b4a?

GaryOCO

Member
Licensed User
Longtime User
IF I get a user to type in a date or submit a date string to a sub routine, I need to know, or be able to check, that a correct date string has been entered.

In VB I can check if a string is a legitimate date by calling the 'IsDate' function...

If IsDate(DateString) Then 'Check that DateString is a legitimate date string
'Do something
Else
'Do something else
End IF

I can also format a date string using the 'Format' function...

DateString = Format(Now,"dd/MMM/yyyy")

Can these results be achieved easily in B4A?
 

mc73

Well-Known Member
Licensed User
Longtime User
Irrelevant a bit, but in such occasions, I find it appropriate to use the dateDialog.
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
I can also format a date string using the 'Format' function...

DateString = Format(Now,"dd/MMM/yyyy")

Can these results be achieved easily in B4A?

Yes, for example:

B4X:
Dim now As Long
now = DateTime.now
DateTime.DateFormat="dd/MM/yyyy"
DateString = DateTime.Date(now)

Sergio
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
The solution I have been using is very similar to Erels. The sub I came up with was.

B4X:
Public Sub IsDate (date As String) As Boolean
   Dim dateticks As Long
   
   date = date.Replace(".", "/")
   date = date.Replace("-", "/")
   date = date.Replace("\", "/")
   
      Try
         dateticks = DateTime.DateParse(date)
         If DateTime.GetYear(dateticks) > 1901 Then
            Return True
         Else
            Return False
         End If
      Catch
         Return False
      End Try
End Sub

The Replace statements allow other separators to be used '.', '-' or '\'. A but more tolerant to user input.

The Year Check if statement can be removed, that is just in as this particular app needed it ;)

Seems to work fine for me.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Hi barx,
I think yours isn't always corrected,if it's "2013/02/28" or "28/02/2013"
B4X:
Public Sub IsDate (date As String) As Boolean
   Dim dateticks As Long
      Try
                     dateticks=Format(DateTime.DateParse(date),"dd/MMM/yyyy")
             If DateTime.GetYear(dateticks) > 1901 Then
            Return True
           Else
            Return False
          End If
      Catch
         Return False
      End Try
End Sub
 
Last edited:
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
True, thanks for pointing that out. In my app I have a hint showing format. That little tweak would also allow other formats too though so will ad it ;)
 
Upvote 0

GaryOCO

Member
Licensed User
Longtime User
Brand new to this

Thank you Erel I will remember to post to the correct blog but I did post it in the wish list because I wish these functions were available.

I would like to thank all of you that got involved, this is my first post as a member and it is heartening to see such support.

Obviously the easy method is going to be to use the DateDialog as suggested by MC73 but I was hoping to be able to achieve these things without having to do too much programming.

After you have spent as long as I have programming in Visual Studio you get used to having your familiar tools. I guess I'll have to adjust. B4A looks great, I've had it for a week and am about to release my first app which is, in fact, a replica of one of the modules from our commercial Windows programme.

The only real problem I am experiencing is getting the views to look right on every device.

Thank you all again, I'll work with what you have suggested.
 
Upvote 0
Top