Date math

rfresh

Well-Known Member
Licensed User
Longtime User
I was looking at the Users Guide but still can't figure out how to parse dates to get how many days. I want to set a date (Long) when my app first installs then check this date against today's date to determine if the trial period has expired. I want a 30 day trial period.

Do I use the

B4X:
DateTime.Add(DateTime.Now, 0, 0, 1)

method?

Thanks...
 

Jost aus Soest

Active Member
Licensed User
Longtime User
All dates and timestamps in B4A are internally stored in Ticks, i. e. a long number representing the time/date in milliseconds. So you can use dates/times like numbers and can calc differences using simply math.

Example:
B4X:
DateNow = ...
DateInstalled = ...
NumberOfDays = (DateNow - DateInstalled) / 86400000
If NumberOfDays > 30 Then
  'Trial period expired...
Else
  'Valid trial period...
End If

(BTW: A day has 86400 seconds...)
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User

This is covered in the Documentation Wiki which also contains a link to a thread which gets into other considerations.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If you want to use your datetime.add function, here is a method that yields the same result as Jost and Klaus, but with a different flavor:
B4X:
'BELOW To CALCULATE IF 30 DAY TRIAL PERIOD EXPIRED
Dim DateInstalled As String
DateInstalled = "4/20/2012"
If DateTime.Add(DateTime.dateparse(DateInstalled),0,0,30)< DateTime.Now Then
'Trial period expired...
   Msgbox("Expired","")
Else
  'Valid trial period...
   Msgbox("Not Expired","")
End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…