Android Question AnotherDatePicker question

mrjaw

Active Member
Licensed User
Longtime User
Hi!
I am using ANotherDatePicker for take a date from my phone. I have a problem when I take a date in the past . To check if the date is in the past I do this
B4X:
Sub ADP_Closed (Cancelled As Boolean, Date As Long)



If Cancelled = False AND Date < DateTime.Now Then

    'future time

    Msgbox("Wrong Date ","test")

    ADP.SetDate(DateTime.Now, True)

Else

    Dim s As String

    DateTime.DateFormat="yyyy-MM-dd"

    s= DateUtils.TicksToString(Date)

    s= s.SubString2(0,s.IndexOf(" "))

    Msgbox(s,"test")

EndIf


Log("Cancelled = " & Cancelled & ", Date = " & DateUtils.TicksToString(Date))

End Sub

When I take the same day it told me that is wrong date because it uses timestamp so the date from ANotherPicker is YYYY-MM-DD 00:00 so it is less than the date taken.

There is a way to take just date without time to compare only dates without hours ?


TIA
 

mangojack

Expert
Licensed User
Longtime User
there might be a better way .. the sub code courtesy of Ricky D .. also no need to use substring2 to format your string to date only ...

B4X:
Sub ADP_Closed (Cancelled As Boolean, Date As Long)
  
   If Cancelled = False AND StripTimeOffDate(Date) < StripTimeOffDate(DateTime.now) Then

     'future time
     Msgbox("Wrong Date ","test")
     ADP.SetDate(DateTime.now, True)

   Else
     Dim s As String
     DateTime.DateFormat="yyyy-MM-dd"
     s= DateTime.Date(Date)
     Msgbox(s,"test")
  
   End If

   Log("Cancelled = " & Cancelled & ", Date = " & DateUtils.TicksToString(Date))

End Sub

Sub StripTimeOffDate(date As Long) As Long
  
  DateTime.SetTimeZone(0)
  Dim time As Long
  time = date Mod DateTime.TicksPerDay
  
  Return date - time
  
End Sub
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
another way ..
B4X:
Sub ADP_Closed (Cancelled As Boolean, Date As Long)
  
   Dim Today  As Long  = DateTime.DateParse(DateTime.Date(DateTime.Now))
   Date = DateTime.DateParse(DateTime.Date(Date))
   If Cancelled = False AND Date < Today Then

  '...........................
 
Last edited:
Upvote 0
Top