Android Question Date difference Problem

Andreas_D99

Member
Licensed User
With this Code I try to get the Difference in Days between two Dates:

B4X:
...
Dim lngToday As Long
Dim lngNewDate As Long
Dim lngDiff As Long
Dim DaysLeft As Long

lngToday = DateTime.DateParse(DateTime.Date(DateTime.Now))               
lngNewDate = DateTime.DateParse(04/05/2017)
lngDiff = lngNewDate - lngToday
DaysLeft = lngDiff / DateTime.TicksPerDay
...
When lngToday is 22/03/2017 and lngNewDate is 04/05/2017 the Difference is 14 Days.
But When between this 2 Dates is a Sunday, the result of variable "DaysLeft" is one day less

Only when the current date has exceeded Sunday, the correct difference is displayed.

Where is my Error ??
 

Andreas_D99

Member
Licensed User
Hello Erel,
now I use DateUtils.PeriodBetween, but the same Problem.
Difference between Today and 05. April 2017 will be shown as 13 Days... ??
B4X:
Sub Activity_Create(FirstTime As Boolean)
   ToastMessageShow("Check the logs...", True)
   Log("Today is " & DateUtils.GetDayOfWeekName(DateTime.Now))
   Log("There are " & DateUtils.NumberOfDaysInMonth(DateTime.GetMonth(DateTime.Now), _
     DateTime.GetYear(DateTime.Now)) & " days in this month.")
   Dim targetDate As Long = DateUtils.SetDate(2017, 4, 5)
   Dim timeToDate As Period = DateUtils.PeriodBetween(DateTime.Now, targetDate)
   Log(timeToDate)
   Log("Time left to 2017/04/05: " & timeToDate.Months & " months, " & timeToDate.Days & " days")
End Sub

Is this a Problem with the German Date Format ?
 
Upvote 0

Andreas_D99

Member
Licensed User
it will be allways left the Sunday, and between 14 Days are max. 2 Sundays, so I use this:
B4X:
If DateTime.GetDayOfMonth(CurrentDate) = 1 Then
       timeToDate.Days = timeToDate.Days+ 2
     Else
       timeToDate.Days = timeToDate.Days+ 1
     End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
DateTime.DateParse(04/05/2017)
DateParse needs a STRING. The value can not be correct.

B4X:
    Dim lngToday As Long
    Dim lngNewDate As Long
    'Log(DateTime.Date(DateTime.Now))
    'lngToday = DateTime.DateParse(DateTime.Date(DateTime.Now))
    lngToday = DateTime.DateParse("04/05/2017")
    lngNewDate = DateTime.DateParse("03/22/2017")
   
    Dim p As Period
    p = DateUtils.PeriodBetween(lngNewDate,lngToday)
    Log(p)


** Activity (main) Create, isFirst = true **[Days=14, Hours=0, IsInitialized=true
, Minutes=0, Months=0, Seconds=0
, Years=0]
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…