Author Topic: Date Picker: Finished  (Read 3500 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Date Picker: Finished
« on: May 11, 2020, 03:47:04 pm »
EDIT: So ok, I lied, I went through the painstaking process of finding C++ and Javascript functions for date calculation and converted them to QB64 code. Here is the ACTUAL finished product.
Here are the two links for the borrowed Javascript and C++ code: https://www.codespeedy.com/find-difference-between-two-dates-in-cpp/ and https://stackoverflow.com/a/13627586/4771393 and of course an edited version of the WeekDay$ function found in the help menu for DATE$.
Here is a finished version of the date picker I recently announced. It allows you to bring up a Windows Forms date picker and the script returns back the date using $LASTEXITCODE from PowerShell rather than outputting to a text file then reading it. Here is the code for the function that you can $INCLUDE at the end of your program as a BM to call a date picker object up whenever you feel like it. It returns the date formatted as MM-dd-yyyy. Maybe if someone wants to go through all the hullabaloo of using mathematics to compare the current date with the date that you pick then that would be a huge upgrade. I, however, don't feel like getting involved with a bunch of math.
Code: QB64: [Select]
  1. 'OPTION _EXPLICIT
  2. DIM date1 AS STRING
  3. DIM date2 AS STRING
  4.  
  5. date1 = GetDate
  6. date2 = GetDate
  7. PRINT "The difference between "; date1$; " and "; date2$; " is "; RTRIM$(LTRIM$(STR$(difference_of_days(date1, date2)))); " days"
  8. PRINT WeekDay(date1)
  9. PRINT WeekDay(date2)
  10.  
  11. FUNCTION GetDate$
  12.     DIM SHELL$
  13.     DIM a AS LONG
  14.     DIM datepicked AS STRING
  15.     DIM month AS STRING
  16.     DIM day AS STRING
  17.     DIM year AS STRING
  18.     SHELL$ = "PowerShell -WindowStyle Hidden -ExecutionPolicy Bypass " + CHR$(34) + "&'" + _STARTDIR$ + "\GetNewDate.ps1';exit $LASTEXITCODE" + CHR$(34)
  19.     a = _SHELLHIDE(SHELL$)
  20.         CASE 7
  21.             datepicked = LTRIM$(STR$(a))
  22.             month = "0" + MID$(datepicked, 1, 1)
  23.             day = MID$(datepicked, 2, 2)
  24.             year = MID$(datepicked, 4, 4)
  25.         CASE 8
  26.             datepicked = LTRIM$(STR$(a))
  27.             month = MID$(datepicked, 1, 2)
  28.             day = MID$(datepicked, 3, 2)
  29.             year = MID$(datepicked, 5, 4)
  30.     END SELECT
  31.     IF datepicked <> "" THEN
  32.         GetDate = month + "-" + day + "-" + year
  33.     ELSE
  34.         GetDate = ""
  35.     END IF
  36.  
  37. FUNCTION WeekDay$ (formatteddate AS STRING)
  38.     DIM M
  39.     DIM D
  40.     DIM Y
  41.     DIM C
  42.     DIM S1
  43.     DIM S2
  44.     DIM S3
  45.     DIM WkDay
  46.     DIM day AS STRING
  47.     DIM Moon AS STRING
  48.     M = VAL(MID$(formatteddate, 1, 2))
  49.     D = VAL(MID$(formatteddate, 4, 2))
  50.     Y = VAL(MID$(formatteddate, 7, 4))
  51.  
  52.     IF M < 3 THEN M = M + 12: Y = Y - 1 'add 12 to Jan - Feb month, -1 year
  53.     C = Y \ 100: Y = Y MOD 100 'split century and year number
  54.     S1 = (C \ 4) - (2 * C) - 1 'century leap
  55.     S2 = (5 * Y) \ 4 '4 year leap
  56.     S3 = 26 * (M + 1) \ 10 'days in months
  57.     WkDay = (S1 + S2 + S3 + D) MOD 7 'weekday total remainder
  58.     IF WkDay < 0 THEN WkDay = WkDay + 7 'Adjust negative results to 0 to 6
  59.     SELECT CASE WkDay
  60.         CASE 0: day = "Sunday"
  61.         CASE 1: day = "Monday"
  62.         CASE 2: day = "Tuesday"
  63.         CASE 3: day = "Wednesday"
  64.         CASE 4: day = "Thursday"
  65.         CASE 5: day = "Friday"
  66.         CASE 6: day = "Saturday"
  67.     END SELECT
  68.     SELECT CASE M
  69.         CASE 1: Moon = "January"
  70.         CASE 2: Moon = "February"
  71.         CASE 3: Moon = "March"
  72.         CASE 4: Moon = "April"
  73.         CASE 5: Moon = "May"
  74.         CASE 6: Moon = "June"
  75.         CASE 7: Moon = "July"
  76.         CASE 8: Moon = "August"
  77.         CASE 9: Moon = "September"
  78.         CASE 10: Moon = "October"
  79.         CASE 11: Moon = "November"
  80.         CASE 12: Moon = "December"
  81.     END SELECT
  82.     WeekDay$ = day + ", " + Moon + " " + ordinal_suffix_of(LTRIM$(RTRIM$(STR$(D)))) + ", " + MID$(formatteddate, 7, 4)
  83.  
  84. FUNCTION ordinal_suffix_of$ (i AS STRING)
  85.     DIM j
  86.     DIM k
  87.     j = VAL(i) MOD 10
  88.     k = VAL(i) MOD 100
  89.     IF j = 1 AND k <> 11 THEN
  90.         ordinal_suffix_of = i + "st"
  91.     ELSEIF j = 2 AND k <> 12 THEN
  92.         ordinal_suffix_of = i + "nd"
  93.     ELSEIF j = 3 AND k <> 13 THEN
  94.         ordinal_suffix_of = i + "rd"
  95.     ELSE
  96.         ordinal_suffix_of = i + "th"
  97.     END IF
  98.  
  99. FUNCTION check_leap_year (year AS LONG)
  100.     IF year MOD 4 = 0 AND year MOD 100 <> 0 OR year MOD 400 = 0 THEN
  101.         check_leap_year = 1
  102.     ELSE
  103.         check_leap_year = 0
  104.     END IF
  105.  
  106. FUNCTION no_of_days_in_month (month AS LONG, year AS LONG)
  107.     IF month = 1 OR month = 3 OR month = 5 OR month = 7 OR month = 8 OR month = 10 OR month = 12 THEN
  108.         no_of_days_in_month = 31
  109.     ELSEIF month = 4 OR month = 6 OR month = 9 OR month = 11 THEN
  110.         no_of_days_in_month = 30
  111.     ELSEIF month = 2 THEN
  112.         DIM n AS LONG
  113.         n = check_leap_year(year)
  114.         IF n = 1 THEN
  115.             no_of_days_in_month = 29
  116.         ELSE
  117.             no_of_days_in_month = 28
  118.         END IF
  119.     END IF
  120.  
  121. FUNCTION difference_of_days& (Date1 AS STRING, Date2 AS STRING)
  122.     DIM month1
  123.     DIM day1
  124.     DIM year1
  125.     DIM month2
  126.     DIM day2
  127.     DIM year2
  128.  
  129.     month1 = VAL(MID$(Date1, 1, 2))
  130.     day1 = VAL(MID$(Date1, 4, 2))
  131.     year1 = VAL(MID$(Date1, 7, 4))
  132.  
  133.     month2 = VAL(MID$(Date2, 1, 2))
  134.     day2 = VAL(MID$(Date2, 4, 2))
  135.     year2 = VAL(MID$(Date2, 7, 4))
  136.  
  137.     IF year1 = year2 THEN
  138.         IF month1 = month2 THEN
  139.             IF day1 = day2 THEN
  140.                 difference_of_days = 0
  141.                 EXIT FUNCTION
  142.             ELSE
  143.                 difference_of_days = ABS(day1 - day2)
  144.                 EXIT FUNCTION
  145.             END IF
  146.         ELSEIF month1 < month2 THEN
  147.             DIM result AS LONG
  148.             DIM i
  149.             FOR i = month1 TO month2 - 1
  150.                 result = result + no_of_days_in_month(i, year1)
  151.             NEXT
  152.             IF day1 = day2 THEN
  153.                 difference_of_days = result
  154.                 EXIT FUNCTION
  155.             ELSEIF day1 < day2 THEN
  156.                 result = result + (day2 - day1)
  157.                 difference_of_days = result
  158.                 EXIT FUNCTION
  159.             ELSE
  160.                 result = result - (day1 - day2)
  161.                 difference_of_days = result
  162.                 EXIT FUNCTION
  163.             END IF
  164.         ELSE
  165.             result = 0
  166.             FOR i = month2 TO month1 - 1
  167.                 result = result + no_of_days_in_month(i, year1)
  168.             NEXT
  169.             IF day1 = day2 THEN
  170.                 difference_of_days = result
  171.                 EXIT FUNCTION
  172.             ELSEIF day2 < day1 THEN
  173.                 result = result + (day1 - day2)
  174.                 difference_of_days = result
  175.                 EXIT FUNCTION
  176.             ELSE
  177.                 result = result - (day2 = day1)
  178.                 difference_of_days = result
  179.                 EXIT FUNCTION
  180.             END IF
  181.         END IF
  182.     ELSEIF year1 < year2 THEN
  183.         DIM temp AS LONG
  184.         FOR i = year1 TO year2 - 1
  185.             IF check_leap_year(i) = 1 THEN
  186.                 temp = temp + 366
  187.             ELSE
  188.                 temp = temp + 365
  189.             END IF
  190.         NEXT
  191.         IF month1 = month2 THEN
  192.             IF day1 = day2 THEN
  193.                 difference_of_days = temp
  194.                 EXIT FUNCTION
  195.             ELSEIF day1 < day2 THEN
  196.                 difference_of_days = temp + (day2 - day1)
  197.                 EXIT FUNCTION
  198.             ELSE
  199.                 difference_of_days = temp - (day1 - day2)
  200.                 EXIT FUNCTION
  201.             END IF
  202.         ELSEIF month1 < month2 THEN
  203.             result = 0
  204.             FOR i = month1 TO month2 - 1
  205.                 result = result + no_of_days_in_month(i, year2)
  206.             NEXT
  207.             IF day1 = day2 THEN
  208.                 difference_of_days = temp + result
  209.                 EXIT FUNCTION
  210.             ELSEIF day1 < day2 THEN
  211.                 result = result + (day2 - day1)
  212.                 difference_of_days = temp + result
  213.                 EXIT FUNCTION
  214.             ELSE
  215.                 result = result - (day1 - day2)
  216.                 difference_of_days = temp + result
  217.                 EXIT FUNCTION
  218.             END IF
  219.         ELSE
  220.             result = 0
  221.             FOR i = month2 TO month1 - 1
  222.                 result = result + no_of_days_in_month(i, year2)
  223.             NEXT
  224.             IF day1 = day2 THEN
  225.                 difference_of_days = temp - result
  226.                 EXIT FUNCTION
  227.             ELSEIF day2 < day2 THEN
  228.                 result = result + (day1 - day2)
  229.                 difference_of_days = temp - result
  230.                 EXIT FUNCTION
  231.             ELSE
  232.                 result = result - (day2 - day1)
  233.                 difference_of_days = temp - result
  234.                 EXIT FUNCTION
  235.             END IF
  236.         END IF
  237.     ELSE
  238.         temp = 0
  239.         FOR i = year2 TO year1 - 1
  240.             IF check_leap_year(i) = 1 THEN
  241.                 temp = temp + 366
  242.             ELSE
  243.                 temp = temp + 365
  244.             END IF
  245.         NEXT
  246.         IF month1 = month2 THEN
  247.             IF day1 = day2 THEN
  248.                 difference_of_days = temp
  249.                 EXIT FUNCTION
  250.             ELSEIF day2 < day1 THEN
  251.                 difference_of_days = temp + (day1 - day2)
  252.                 EXIT FUNCTION
  253.             ELSE
  254.                 difference_of_days = temp - (day2 - day1)
  255.                 EXIT FUNCTION
  256.             END IF
  257.         ELSEIF month2 < month1 THEN
  258.             result = 0
  259.             FOR i = month2 TO month1 - 1
  260.                 result = result + no_of_days_in_month(i, year1)
  261.             NEXT
  262.             IF day1 = day2 THEN
  263.                 difference_of_days = temp + result
  264.                 EXIT FUNCTION
  265.             ELSEIF day2 < day1 THEN
  266.                 result = result + (day1 - day2)
  267.                 difference_of_days = temp + result
  268.                 EXIT FUNCTION
  269.             ELSE
  270.                 result = result - (day2 - day1)
  271.                 difference_of_days = temp + result
  272.                 EXIT FUNCTION
  273.             END IF
  274.         ELSE
  275.             result = 0
  276.             FOR i = month1 TO month2 - 1
  277.                 result = result + no_of_days_in_month(i, year1)
  278.             NEXT
  279.             IF day1 = day2 THEN
  280.                 difference_of_days = temp - result
  281.                 EXIT FUNCTION
  282.             ELSEIF day1 < day2 THEN
  283.                 result = result + (day2 - day1)
  284.                 difference_of_days = temp - result
  285.                 EXIT FUNCTION
  286.             ELSE
  287.                 result = result - (day1 - day2)
  288.                 difference_of_days = temp - result
  289.                 EXIT FUNCTION
  290.             END IF
  291.         END IF
  292.     END IF
« Last Edit: May 12, 2020, 03:32:21 pm by SpriggsySpriggs »
Shuwatch!