I previously posted this but later found an error. It takes two strings containing the time and date. It assumes the first string is the earlier of the two times. The time strings are in the format with one or two digits for hours, month and day. Assumes the time is in 12 hour time. For example, one valid string is 1:25 PM 12/7/2021
FUNCTION GetDifference
(earlierTime$
, moreRecentTime$
) colonPosition1
= INSTR(earlierTime$
, ":") 'format is H:MM #M M/D/YYYY to HH:MM #M MM/DD/YYYY so finging the colon is needed colonPosition2
= INSTR(moreRecentTime$
, ":") 'same hours1
= VAL(LEFT$(earlierTime$
, colonPosition1
- 1)) 'hours portion of the time hours2
= VAL(LEFT$(moreRecentTime$
, colonPosition2
- 1)) minutes1
= VAL(MID$(earlierTime$
, colonPosition1
+ 1, 2)) ' minutes portion of the time minutes2
= VAL(MID$(moreRecentTime$
, colonPosition2
+ 1, 2)) IF MID$(earlierTime$
, colonPosition1
+ 4, 1) = "P" AND hours1
<> 12 THEN hours1
= hours1
+ 12 'convert to 24hr IF MID$(moreRecentTime$
, colonPosition2
+ 4, 1) = "P" AND hours2
<> 12 THEN hours2
= hours2
+ 12 month1
= VAL(LTRIM$(MID$(earlierTime$
, INSTR(earlierTime$
, "/") - 2, 2))) 'the month of the earlier string month2
= VAL(LTRIM$(MID$(moreRecentTime$
, INSTR(moreRecentTime$
, "/") - 2, 2))) 'month of the later string rightCut1$
= MID$(earlierTime$
, INSTR(earlierTime$
, "/") + 1, LEN(earlierTime$
)) 'cut the string to find the 2nd "/" rightcut2$
= MID$(moreRecentTime$
, INSTR(moreRecentTime$
, "/") + 1, LEN(moreRecentTime$
)) day1
= VAL(LEFT$(rightCut1$
, INSTR(rightCut1$
, "/") - 1)) 'get the day of the month year1
= VAL(MID$(rightCut1$
, INSTR(rightCut1$
, "/") + 1, 4)) 'and finally the year year2
= VAL(MID$(rightcut2$
, INSTR(rightcut2$
, "/") + 1, 4)) IF hours1
= 24 THEN hours1
= 0 'puts midnight to hour 0 IF hours2
= 24 THEN hours2
= 0 minutesDifferent = 0 'initialize the number of minutes the two times differ
currentMinute = minutes1 'hours, minutes, days, months and years need to stay the same to exit the following loop
currentHour = hours1
currentDay = day1
currentMonth = month1
currentYear = year1
minutesDifferent = minutesDifferent + 1 'increment the number of minutes different
currentMinute = currentMinute + 1 'move the counter for exiting the loop
IF currentMinute
= 60 THEN '60 minutes is the next hour currentMinute = 0
currentHour = currentHour + 1
IF currentHour
= 24 THEN 'go to the next day currentHour = 0
currentDay = currentDay + 1
IF currentDay
>= 28 THEN 'see if the months needs to be incremented and do so when it is needed CASE 4, 6, 9, 11 'April, June, September and November have 30 days IF currentDay
= 31 THEN 'increment the month currentMonth = currentMonth + 1
currentDay = 1
CASE 1, 3, 5, 7, 8, 10, 12 'January, March, May, July, August, October and December IF currentDay
= 32 THEN 'increment the month currentMonth = currentMonth + 1
currentDay = 1
IF currentDay
= 29 AND currentYear
MOD 4 <> 0 THEN 'if not a leap year, increment the month currentMonth = 3
currentDay = 1
ELSEIF currentDay
= 30 AND currentYear
MOD 4 = 0 THEN 'if is leap year, increment month currentMonth = 3
currentDay = 1
IF currentMonth
= 13 THEN 'increment the year currentYear = currentYear + 1
currentMonth = 1
LOOP UNTIL (currentHour
= hours2
AND currentMinute
= minutes2
AND currentMonth
= month2
AND currentDay
= day2
AND currentYear
= year2
) GetDifference = minutesDifferent