Mashiane Expert Licensed User Longtime User Feb 29, 2020 #1 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
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
M Mahares Expert Licensed User Longtime User Feb 29, 2020 #2 Mashiane said: Is there something Im perhaps doing wrong in my formula? Click to expand... Code: 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
Mashiane said: Is there something Im perhaps doing wrong in my formula? Click to expand... Code: 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
OliverA Expert Licensed User Longtime User Feb 29, 2020 #3 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
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
alwaysbusy Expert Licensed User Longtime User Feb 29, 2020 #4 Shouldn’t it be parseInt of the whole division instead of only the iOut? But there seems to be something wrong with the dataParse method, as it returns a different value in BANano than in B4J, I'll investigate Last edited: Feb 29, 2020 Upvote 0
Shouldn’t it be parseInt of the whole division instead of only the iOut? But there seems to be something wrong with the dataParse method, as it returns a different value in BANano than in B4J, I'll investigate
Mashiane Expert Licensed User Longtime User Feb 29, 2020 #5 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
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
alwaysbusy Expert Licensed User Longtime User Feb 29, 2020 #6 Will be fixed in the next update. The temporary solution above complete and done the BANano way: In AppStart include: Code: 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): Code: 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: Code: Log(DateDiff("2020-03-31","2020-02-28")) ' should return 32 Last edited: Feb 29, 2020 Upvote 0
Will be fixed in the next update. The temporary solution above complete and done the BANano way: In AppStart include: Code: 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): Code: 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: Code: Log(DateDiff("2020-03-31","2020-02-28")) ' should return 32
Mashiane Expert Licensed User Longtime User Feb 29, 2020 #7 alwaysbusy said: done the BANano way: Click to expand... Perfect, for now will replace my code. Ta! Upvote 0