Author Topic: Function for the difference in minutes between two times  (Read 3377 times)

0 Members and 1 Guest are viewing this topic.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Function for the difference in minutes between two times
« on: July 16, 2021, 02:21:23 pm »
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
Code: QB64: [Select]
  1. FUNCTION GetDifference (earlierTime$, moreRecentTime$)
  2.     colonPosition1 = INSTR(earlierTime$, ":") 'format is H:MM #M M/D/YYYY to HH:MM #M MM/DD/YYYY so finging the colon is needed
  3.     colonPosition2 = INSTR(moreRecentTime$, ":") 'same
  4.     hours1 = VAL(LEFT$(earlierTime$, colonPosition1 - 1)) 'hours portion of the time
  5.     hours2 = VAL(LEFT$(moreRecentTime$, colonPosition2 - 1))
  6.     minutes1 = VAL(MID$(earlierTime$, colonPosition1 + 1, 2)) ' minutes portion of the time
  7.     minutes2 = VAL(MID$(moreRecentTime$, colonPosition2 + 1, 2))
  8.     IF MID$(earlierTime$, colonPosition1 + 4, 1) = "P" AND hours1 <> 12 THEN hours1 = hours1 + 12 'convert to 24hr
  9.     IF MID$(moreRecentTime$, colonPosition2 + 4, 1) = "P" AND hours2 <> 12 THEN hours2 = hours2 + 12
  10.     month1 = VAL(LTRIM$(MID$(earlierTime$, INSTR(earlierTime$, "/") - 2, 2))) 'the month of the earlier string
  11.     month2 = VAL(LTRIM$(MID$(moreRecentTime$, INSTR(moreRecentTime$, "/") - 2, 2))) 'month of the later string
  12.     rightCut1$ = MID$(earlierTime$, INSTR(earlierTime$, "/") + 1, LEN(earlierTime$)) 'cut the string to find the 2nd "/"
  13.     rightcut2$ = MID$(moreRecentTime$, INSTR(moreRecentTime$, "/") + 1, LEN(moreRecentTime$))
  14.     day1 = VAL(LEFT$(rightCut1$, INSTR(rightCut1$, "/") - 1)) 'get the day of the month
  15.     day2 = VAL(LEFT$(rightcut2$, INSTR(rightcut2$, "/") - 1))
  16.     year1 = VAL(MID$(rightCut1$, INSTR(rightCut1$, "/") + 1, 4)) 'and finally the year
  17.     year2 = VAL(MID$(rightcut2$, INSTR(rightcut2$, "/") + 1, 4))
  18.     IF hours1 = 24 THEN hours1 = 0 'puts midnight to hour 0
  19.     IF hours2 = 24 THEN hours2 = 0
  20.     minutesDifferent = 0 'initialize the number of minutes the two times differ
  21.     currentMinute = minutes1 'hours, minutes, days, months and years need to stay the same to exit the following loop
  22.     currentHour = hours1
  23.     currentDay = day1
  24.     currentMonth = month1
  25.     currentYear = year1
  26.     DO
  27.         minutesDifferent = minutesDifferent + 1 'increment the number of minutes different
  28.         currentMinute = currentMinute + 1 'move the counter for exiting the loop
  29.         IF currentMinute = 60 THEN '60 minutes is the next hour
  30.             currentMinute = 0
  31.             currentHour = currentHour + 1
  32.             IF currentHour = 24 THEN 'go to the next day
  33.                 currentHour = 0
  34.                 currentDay = currentDay + 1
  35.                 IF currentDay >= 28 THEN 'see if the months needs to be incremented and do so when it is needed
  36.                     SELECT CASE currentMonth
  37.                         CASE 4, 6, 9, 11 'April, June, September and November have 30 days
  38.                             IF currentDay = 31 THEN 'increment the month
  39.                                 currentMonth = currentMonth + 1
  40.                                 currentDay = 1
  41.                             END IF
  42.                         CASE 1, 3, 5, 7, 8, 10, 12 'January, March, May, July, August, October and December
  43.                             IF currentDay = 32 THEN 'increment the month
  44.                                 currentMonth = currentMonth + 1
  45.                                 currentDay = 1
  46.                             END IF
  47.                         CASE 2 'february
  48.                             IF currentDay = 29 AND currentYear MOD 4 <> 0 THEN 'if not a leap year, increment the month
  49.                                 currentMonth = 3
  50.                                 currentDay = 1
  51.                             ELSEIF currentDay = 30 AND currentYear MOD 4 = 0 THEN 'if is leap year, increment month
  52.                                 currentMonth = 3
  53.                                 currentDay = 1
  54.                             END IF
  55.                     END SELECT
  56.                     IF currentMonth = 13 THEN 'increment the year
  57.                         currentYear = currentYear + 1
  58.                         currentMonth = 1
  59.                     END IF
  60.                 END IF
  61.             END IF
  62.         END IF
  63.     LOOP UNTIL (currentHour = hours2 AND currentMinute = minutes2 AND currentMonth = month2 AND currentDay = day2 AND currentYear = year2)
  64.     GetDifference = minutesDifferent
  65.