Subtracting one date from another

Inman

Well-Known Member
Licensed User
Longtime User
Visual Basic has this function called Datediff that shows number of days between 2 dates. I am looking for a similar function in B4A. I see that B4A has DateTime.Add but it sounds more like adding (or subtracting) days to a certain date to get the new date. I need the reverse. Is it possible?
 

rbsoft

Active Member
Licensed User
Longtime User
Yes. From the helpfiles (B4A Helpviewer):

Pass negative values if you want to subtract the values.
Example:
B4X:
Dim Tomorrow As Long
Tomorrow = DateTime.Add(DateTime.Now, 0, 0, 1)
Log("Tomorrow date is: " & DateTime.Date(Tomorrow))
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Thank you, but that doesn't accept 2 days and show their difference. For example I have 25/11/2011 and 26/11/2011. I need a function to subtract 25/11/2011 from 26/11/2011 so that the answer is 1. In DateTime.Add, I can only subtract 1 from 26/11/2011 so that the answer is 25/11/2011. I need the opposite.
 
Upvote 0

rbsoft

Active Member
Licensed User
Longtime User
Ok, I understand. That also is not too difficult. You could use a function like this;


B4X:
Dim tks, d, h, m, s As Long 

tks = DateTime.DateParse("06/15/2011") - DateTime.DateParse("06/05/2011")
d = tks / DateTime.TicksPerDay 
   
ToastMessageShow(d, True)

The result will show you that the difference is 10 days.

Rolf
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Somehow I missed TicksPerDay when I checked out the documentation. I was wondering all along how to reverse a tick to readable date. Thanks for the help mate.
 
Upvote 0
Top