Android Question Why cannot get the months elapsed?

morphe2011

Member
Licensed User
Longtime User
It has been years since I've needed help. I must be getting old.

DateTime.DateFormat = "MM/dd/yyyy"
Dim targetDate As Long = DateUtils.SetDate(2019, 8, 1)
Dim mPeriod As Period =DateUtils.PeriodBetween(DateTime.Now, targetDate)

I want to know how many months have elapsed since August of 2019. What I get is 1.
 

aeric

Expert
Licensed User
Longtime User
B4X:
Sub CountMonths
    DateTime.DateFormat = "MM/dd/yyyy"
    Dim targetDate As Long = DateUtils.SetDate(2019, 8, 1)
    Dim mPeriod As Period = DateUtils.PeriodBetween(targetDate, DateTime.Now)
    Dim monthsElapsed As Int = (mPeriod.Years * 12) + mPeriod.Months
    Log($"Total Months since ${DateTime.Date(targetDate)} is ${monthsElapsed} months"$)
End Sub
 
Upvote 1

DonManfred

Expert
Licensed User
Longtime User
Period has values for days, months, years. Look at all of them!

B4X:
dim months as int = (mPeriod.years*12)+mPeriod.Months
 
Upvote 0
Top