Date Condition

junaidahmed

Well-Known Member
Licensed User
Longtime User
I have two string variable A & B that contains Data value,if I check the Less condition for two variable it show an error "NumberFormatException".Pls advise where it is problem.my code is below

A = "2011-06-01"
B = "2011-06-21"

if A < B Then
" Error Message : NumberFormatException"
End if
 

JMB

Active Member
Licensed User
Longtime User
Although your strings contain dates, they are still strings.

You might be better to use DateParse(date as string) which will convert the date in the string into a long. If you do that with both strings, the comparison will then work.

See the DateParse() documentation in the Core library.

JMB
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
JMB is right, here an example:

B4X:
   Dim a1 As Long, b1 As Long
   Dim A As String, B As String
   DateTime.DateFormat = "yyyy-MM-dd"  '!IMPORTANT
   A = "2011-06-01"
   B = "2011-06-21"
   a1 = DateTime.DateParse(A)
   b1 = DateTime.DateParse(B)
   If a1 < b1 Then
      Msgbox("A is an earlier date.","Result")
   End If

or shorter:

B4X:
   If DateTime.DateParse("2011-06-01") < DateTime.DateParse("2011-06-21") Then
      Msgbox("First half of June.","Result")
   End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…