Android Question Calculate Difference Between Two Date Strings?

Big Dave

Member
Licensed User
Longtime User
I am trying to calculate the difference in hours and minutes between two date/time strings, both in the format "yyMMddHHmm". I have been trying to use DateUtils but no matter what I do it always returns zero for all return values.

B4X:
    Dim lngTimeFrom As Long
    Dim lngTimeTo As Long
    Dim TimeDiff As Period
    TimeDiff.Initialize

Above code is at start of sub.

B4X:
DateTime.DateFormat = "yyMMddHHmm"
            DateTime.TimeFormat = "yyMMddHHmm"
            lngTimeFrom = DateTime.DateTimeParse(strXFromDateTime, strXFromDateTime)
            lngTimeTo =  DateTime.DateTimeParse(strXToDateTime, strXToDateTime)
            Log(lngTimeFrom)
            Log(lngTimeTo)
            DateUtils.PeriodBetween(lngTimeFrom, lngTimeTo)
            Log(TimeDiff.Years)
            Log(TimeDiff.Months)
            Log(TimeDiff.Days)
            Log(TimeDiff.Hours)
            Log(TimeDiff.Minutes)
            Log(TimeDiff.Seconds)

Above code converts both dates to ticks and calls DateUtils PeriodBetween routine.

No matter what I do all values returned are zero so I think I am missing something.

Thanks.
 

Big Dave

Member
Licensed User
Longtime User
RESOLVED

I actually had two problems:

1. I neglected to add "TimeDiff =" to the front of the DateUtils call. Don't know why this did not throw up an error, compile or otherwise at any point.
2. As both my string date/time fields contained date and time and by doing a DateTimePare I ended up with a time difference of double what it should have been.

The corrected code is below for anyone interested.

B4X:
            DateTime.DateFormat = "yyMMddHHmm"
            TimeDiff = DateUtils.PeriodBetween(DateTime.DateParse(strXFromDateTime), DateTime.DateParse(strXToDateTime))

Thanks to all who took the time to read the post.
 
Upvote 0
Top