Simulating VB6 DateDiff Function In B4A

rtesluk

Member
Licensed User
Longtime User
Jan 9 2012
13:30 Hours

I created a little app to allow me to view the ages of all of my nieces and nephews. Very handy for birthdays and Xmas.

In B4A, I used the no. of ticks for both the currentdate and birthdate and then substracted them like this:

Code: Copy
'Get No. of Ticks of Current Date CurrentDate Long = Primitive Variable
CurrentDate = DateTime.DateParse(DateTime.Date(DateTime.Now))    

'Get No. of Ticks of OtherDate OtherDate Long = Primitive Variable
OtherDate = DateTime.DateParse(sDate) 

'Subtract OtherDate Ticks FROM CurrentDate Ticks Divide By Total TicksPerDay Then No. of Days Per Year = 365 Usually Leap Year 366
NumOfYears = (CurrentDate - OtherDate)/(DateTime.TicksPerDay)/365.25

I think that's the same thing in VB6 as

DateDiff("yyyy", OtherDate, Now())

then for 'months, I used the following code; namely,

Code: Copy
'Subtract OtherDate Ticks FROM CurrentDate Ticks Divide By Total TicksPerDay Then No. of Months Per Year Add Up Days/Month And Take The Average = 30.42
NumOfMonths = (CurrentDate - OtherDate)/(DateTime.TicksPerDay)/30.42

I think that's the same thing in VB6 as

DateDiff("m", OtherDate, Now())

then for 'days', you could use the following:

Code: Copy
'Subtract OtherDate Ticks FROM CurrentDate Ticks Divide By Total TicksPerDay
NumOfMonths = (CurrentDate - OtherDate)/(DateTime.TicksPerDay)

I think that's the same thing in VB6 as

DateDiff("d", OtherDate, Now())

I have uploaded a sample app for your perusal.

Any comments would be appreciated!

Ray Tesluk :sign0104:
Port Hope ON
Canada
 

Attachments

Last edited:
Hi,

I don't think you may take the average of months and you have to calculate leap years to be completely correct.

Greetings,
Sigurd
 
Upvote 0
Back
Top