Hello,
I am trying to get the difference amount of days from today. Is there a faster way to do it, because I find my method quite slow.
The result I want to get is for example 1 (day) if the date was yesterday, 2 for the day before yesterday. I want the difference in days, starting at midnight, not a 24h difference.
This code works as expected, but I feel it is a bit slow when processing many dates:
What bothers me with this code, is that it seems that there is no other way to get the year, day, month apart from using DateTime.DateFormat.
Thanks
I am trying to get the difference amount of days from today. Is there a faster way to do it, because I find my method quite slow.
The result I want to get is for example 1 (day) if the date was yesterday, 2 for the day before yesterday. I want the difference in days, starting at midnight, not a 24h difference.
This code works as expected, but I feel it is a bit slow when processing many dates:
B4X:
Sub GetDayDifferenceFromToday(Ticks As Long) As Int
Dim OldFormat As String
OldFormat = DateTime.DateFormat
Dim fromDate, toDate As Long
Dim fromYear, fromMonth, fromDay, toYear, toMonth, toDay As Int
DateTime.DateFormat = "yyyy"
fromYear = DateTime.Date(DateTime.Now)
toYear = DateTime.Date(Ticks)
DateTime.DateFormat = "MM"
fromMonth = DateTime.Date(DateTime.Now)
toMonth = DateTime.Date(Ticks)
DateTime.DateFormat = "dd"
fromDay = DateTime.Date(DateTime.Now)
toDay = DateTime.Date(Ticks)
fromDate = DateUtils.SetDate(fromYear, fromMonth, fromDay)
toDate = DateUtils.SetDate(toYear, toMonth, toDay)
Dim Diff As Int
Diff = ((fromDate - toDate) / DateTime.TicksPerDay)
DateTime.DateFormat = OldFormat
Return Diff
End Sub
What bothers me with this code, is that it seems that there is no other way to get the year, day, month apart from using DateTime.DateFormat.
Thanks