B4J Question [BANano] [SOLVED] Incorrect DateDiff Calculations

Mashiane

Expert
Licensed User
Longtime User
For B4J we have dateUtils, so to calculate the DateDiff between two dates in days...

B4X:
Sub DateDiff(CurrentDate As String, OtherDate As String) As Int
    DateTime.DateFormat = "yyyy-MM-dd"
    
    Dim CurrDate As Long
    Dim OthDate As Long
    CurrDate = DateTime.DateParse(CurrentDate)
    OthDate = DateTime.DateParse(OtherDate)
    '
    Log("*currdate*")
    Log(CurrDate)
    Log("*othdate")
    Log(OthDate)
    Log("*ticks per day")
    Log(DateTime.TicksPerDay)
    Dim iOut As Int = CurrDate - OthDate
    Dim nDays As Int = BANano.parseint(iOut) / DateTime.TicksPerDay
    Return nDays
End Sub

For some reason the result is 3 and not 32

B4X:
current date: 2020-03-31
other date: 2020-02-28
app1582936210201.js:64303 *currdate*
app1582936210201.js:64305 1585605600000
app1582936210201.js:64307 *othdate
app1582936210201.js:64309 1585346400000
app1582936210201.js:64311 *ticks per day
app1582936210201.js:64313 86400000

Is there something Im perhaps doing wrong in my formula?

Thanks
 

Mahares

Expert
Licensed User
Longtime User
Is there something Im perhaps doing wrong in my formula?
B4X:
Log(DateDiff("2020-02-28", "2020-03-31"))  'returns 32   need dateutils lib
Sub DateDiff(CurrentDate As String, OtherDate As String) As Int
    DateTime.DateFormat = "yyyy-MM-dd"  
    Dim CurrDate As Long
    Dim OthDate As Long
    CurrDate = DateTime.DateParse(CurrentDate)
    OthDate = DateTime.DateParse(OtherDate)
    Dim p As Period = DateUtils.PeriodBetweenInDays(CurrDate, OthDate)
    Return p.days
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I would say that the long to int conversion is loosing a lot of ticks (especially when you subtracting OthDate from CurrDate and stuffing the result in an int) . Keep everything long
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Thanks guys, in the mean time I have effected this solution with the use of moment.js

B4X:
#if javascript
    function datediff(currentDate,otherDate){
        var dateS = moment(currentDate);
        var dateE = moment(otherDate);
        var duration = moment.duration(dateS.diff(dateE));
        var days = duration.asDays();
        return days
    }
#End If

Sub DateDiff(currentDate As String, otherDate As String) As Int
    Dim res As Int = BANAno.RunJavascriptMethod("datediff", Array(currentDate, otherDate))
    Return res
End Sub
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
Will be fixed in the next update. The temporary solution above complete and done the BANano way:

In AppStart include:
B4X:
BANano.Header.AddJavascriptFile("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js")

BANano Method (the duration wrap is not needed):
B4X:
Sub DateDiff(currentDate As String, otherDate As String) As Int    
    Dim dateS, dateE As BANanoObject
    dateS.Initialize4("moment", currentDate)
    dateE.Initialize4("moment", otherDate)
    Return dateS.RunMethod("diff", Array(dateE, "days"))
End Sub

Call as:
B4X:
Log(DateDiff("2020-03-31","2020-02-28")) ' should return 32
 
Last edited:
Upvote 0
Top