Android Question Problem with date differences

Andre Souza

Member
Licensed User
Hello
See this first example :

B4X:
DateTime.dateFormat = "dd/MM/yyyy"
Dim periodo As Period
periodo = DateUtils.PeriodBetween(DateTime.DateParse("31/01/2016"),DateTime.DateParse("28/02/2016"))
Log("Year:" & periodo.Years)
Log("Month:" & periodo.Months)
Log("Days:" & periodo.Days)
End Sub

It returns me 28 days. OK


The problem

B4X:
DateTime.dateFormat = "dd/MM/yyyy"
Dim periodo As Period
periodo = DateUtils.PeriodBetween(DateTime.DateParse("31/01/2016"),DateTime.DateParse("29/02/2016"))
Log("Year:" & periodo.Years)
Log("Month:" & periodo.Months)
Log("Days:" & periodo.Days)
End Sub

It returns me 1 month.
The right was not 29 days. Why a month?
 

Mahares

Expert
Licensed User
Longtime User
If you want to calculate the number of days between the two periods, you need to use the DateUtils.PeriodBetweenInDays method. The year 2016 is a leap year:
B4X:
Dim ndays As Long
periodo = DateUtils.PeriodBetweenInDays(DateTime.DateParse("31/01/2016"),DateTime.DateParse("29/02/2016"))
ndays=periodo.Days
Log(ndays)   'displays 29
 
Upvote 0
Top