Author Topic: Difference in minutes  (Read 2473 times)

0 Members and 1 Guest are viewing this topic.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Difference in minutes
« on: May 02, 2021, 04:25:20 pm »
Here's a function to take two strings of formatted time and return the difference in minutes. The string has to have the format "H:MM ?M M/D/YYYY" Basically when you hit F5 in the windows notepad you get a time stamp with the required format. Hours, day and month can be one or two digits. Spaces are required and it has to be AM or PM, not 24-hour time.

Code: QB64: [Select]
  1. FUNCTION GetDifference (earlierTime$, moreRecentTime$) 'returns minutes
  2.     colonPosition1 = INSTR(earlierTime$, ":")
  3.     colonPosition2 = INSTR(moreRecentTime$, ":")
  4.     hours1 = VAL(LEFT$(earlierTime$, colonPosition1 - 1))
  5.     hours2 = VAL(LEFT$(moreRecentTime$, colonPosition2 - 1))
  6.     minutes1 = VAL(MID$(earlierTime$, colonPosition1 + 1, 2))
  7.     minutes2 = VAL(MID$(moreRecentTime$, colonPosition2 + 1, 2))
  8.     IF MID$(earlierTime$, colonPosition1 + 4, 1) = "P" THEN hours1 = hours1 + 12
  9.     IF MID$(earlierTime$, colonPosition2 + 4, 1) = "P" THEN hours2 = hours2 + 12
  10.     month1 = VAL(LTRIM$(MID$(earlierTime$, INSTR(earlierTime$, "/") - 2, 2)))
  11.     month2 = VAL(LTRIM$(MID$(moreRecentTime$, INSTR(moreRecentTime$, "/") - 2, 2)))
  12.     rightCut1$ = MID$(earlierTime$, INSTR(earlierTime$, "/") + 1, LEN(earlierTime$))
  13.     rightcut2$ = MID$(moreRecentTime$, INSTR(moreRecentTime$, "/") + 1, LEN(moreRecentTime$))
  14.     day1 = VAL(LEFT$(rightCut1$, INSTR(rightCut1$, "/") - 1))
  15.     day2 = VAL(LEFT$(rightcut2$, INSTR(rightcut2$, "/") - 1))
  16.     year1 = VAL(MID$(rightCut1$, INSTR(rightCut1$, "/") + 1, 4))
  17.     year2 = VAL(MID$(rightcut2$, INSTR(rightcut2$, "/") + 1, 4))
  18.     IF hours1 = 24 THEN hours1 = 0
  19.     IF hours2 = 24 THEN hours2 = 0
  20.     IF month1 <> month2 THEN
  21.         SELECT CASE month1
  22.             CASE 9, 4, 6, 11
  23.                 daysInMonth1 = 20
  24.             CASE 1, 3, 5, 7, 8, 10, 12
  25.                 daysInMonth1 = 31
  26.             CASE 2
  27.                 IF year1 MOD 4 = 0 THEN
  28.                     daysInMonth1 = 29
  29.                 ELSE
  30.                     daysInMonth1 = 28
  31.                 END IF
  32.         END SELECT
  33.     END IF
  34.     minutesDifferent = 0
  35.     currentMinute = minutes1
  36.     currentHour = hours1
  37.     currentDay = day1
  38.     currentMonth = month1
  39.     currentYear = year1
  40.     DO
  41.         minutesDifferent = minutesDifferent + 1
  42.         currentMinute = currentMinute + 1
  43.         IF currentMinute = 60 THEN
  44.             currentMinute = 0
  45.             currentHour = currentHour + 1
  46.             IF currentHour = 24 THEN
  47.                 currentHour = 0
  48.                 currentDay = currentDay + 1
  49.                 IF currentDay >= 28 THEN
  50.                     SELECT CASE currentMonth
  51.                         CASE 9, 4, 6, 11
  52.                             IF currentDay = 31 THEN
  53.                                 currentMonth = currentMonth + 1
  54.                                 currentDay = 1
  55.                                 IF currentMonth = 13 THEN
  56.                                     currentYear = currentYear + 1
  57.                                     currentMonth = 1
  58.                                 END IF
  59.                             END IF
  60.                         CASE 1, 3, 5, 7, 8, 10, 12
  61.                             IF currentDay = 32 THEN
  62.                                 currentMonth = currentMonth + 1
  63.                                 currentDay = 1
  64.                                 IF currentMonth = 13 THEN
  65.                                     currentYear = currentYear + 1
  66.                                     currentMonth = 1
  67.                                 END IF
  68.                             END IF
  69.                         CASE 2
  70.                             IF currentDay = 29 AND currentYear MOD 4 <> 0 THEN
  71.                                 currentMonth = 3
  72.                                 currentDay = 1
  73.                             ELSEIF currentDay = 30 AND currentYear MOD 4 = 0 THEN
  74.                                 currentMonth = 3
  75.                                 currentDay = 1
  76.                             END IF
  77.                     END SELECT
  78.                     IF currentMonth = 13 THEN
  79.                         currentYear = currentYear + 1
  80.                         currentMonth = 1
  81.                     END IF
  82.                 END IF
  83.  
  84.             END IF
  85.         END IF
  86.     LOOP UNTIL (currentHour = hours2 AND currentMinute = minutes2 AND currentMonth = month2 AND currentDay = day2 AND currentYear = year2)
  87.     GetDifference = minutesDifferent
  88.  

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: Difference in minutes
« Reply #1 on: May 02, 2021, 05:43:47 pm »
Line 23 in the above code should be
Code: QB64: [Select]
  1. daysInMonth1 = 30
, not 20. Didn't notice the typo until I posted the code. I missed it when I tested the code. Sorry.