Author Topic: day of the week function  (Read 3255 times)

0 Members and 1 Guest are viewing this topic.

Offline Larryrl

  • Newbie
  • Posts: 26
    • View Profile
day of the week function
« on: October 22, 2021, 12:32:06 am »
I would like to find a function where I can have it tell me what day of the week it is for the first of the current month. Am wanting to do a computer calendar with slots to write in appointments kind of like a yearly planner.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: day of the week function
« Reply #1 on: October 22, 2021, 12:45:28 am »
Have you tried a search for calendar, there's at least 4 to find:
Pete, SMcNeill, SierraKen, bplus

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: day of the week function
« Reply #2 on: October 22, 2021, 12:49:34 am »
FUNCTION GetDay (m, d, y) 'use 4 digit year
    'From Zeller's congruence: https://en.wikipedia.org/wiki/Zeller%27s_congruence
    mm = m: dd = d: yyyy = y
    IF mm < 3 THEN mm = mm + 12: yyyy = yyyy - 1
    century = yyyy MOD 100
    zerocentury = yyyy \ 100
    result = (dd + INT(13 * (mm + 1) / 5) + century + INT(century / 4) + INT(zerocentury / 4) + 5 * zerocentury) MOD 7
    IF result = 0 THEN
        GetDay = 7
    ELSE
        GetDay = result
    END IF
    'Function changed to return a numeric value instead of a string for this program
    '    SELECT CASE result
    '        CASE 7: GetDay$ = "Saturday"
    '        CASE 1: GetDay$ = "Sunday"
    '        CASE 2: GetDay$ = "Monday"
    '        CASE 3: GetDay$ = "Tuesday"
    '        CASE 4: GetDay$ = "Wednesday"
    '        CASE 5: GetDay$ = "Thursday"
    '        CASE 6: GetDay$ = "Friday"
    '    END SELECT
END FUNCTION
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: day of the week function
« Reply #3 on: October 22, 2021, 11:55:51 am »
The one I posted a few years ago is here: https://www.qb64.org/forum/index.php?topic=855.msg100586#msg100586

It was designed as a popup. I used to toggle to it when I needed to input a date into a WP app.

I checked it. It still runs fine on V2.0. I really should update it to work with the QB64 mouse. Oh well, add that to my to-do list.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: day of the week function
« Reply #4 on: October 22, 2021, 06:33:17 pm »
Here's one based on the doomsday rule that may refer to a hypothetical event according to which the end of human life is at the highest possibility. There are many algorithms written to calculate which day of a week in a year has the highest possibility of doomsday falling on that day.

This program expands upon and is a generalized version of the Dooms Day Code program, where the formula used in combination with the "Special Dates" can easily calculate the Day Of Week (DOW) from a date without any complicated algorithms (such as the one that uses the Julian Date from a specific point in time).

This method is also used as a math trick to calculate the day of week in your head.

See: https://en.wikipedia.org/wiki/Doomsday_rule

Code: QB64: [Select]
  1. _TITLE "Day Of Week"
  2. ' *** Dooms Day Code Used For Calculating The Day of Week ***
  3. ' *** Written by George McGinn
  4. ' *** Code shared under Creative Commons License "Universal 1.0 Public Domain (CC0 1.0)"
  5. ' *** https://creativecommons.org/publicdomain/zero/1.0/
  6. '
  7. ' *** Program will run on Linux, Windows and MAC Systems.
  8. '
  9. '____________________________________________________________________________________________________________________
  10. '
  11. ' Doomsday may refer to a hypothetical event according to which the end of human life is at the highest possibility.
  12. ' There are many algorithms written to calculate which day of a week in a year has the highest possibility of
  13. ' doomsday falling on that day.
  14. '
  15. ' This program expands upon and is a generalized version of the Dooms Day Code program, where the formula used in
  16. ' combination with the "Special Dates" can easily calculate the Day Of Week (DOW) from a date without any complicated
  17. ' algorithms (such as the one that uses the Julian Date from a specific point in time).
  18. '
  19. ' See: https://en.wikipedia.org/wiki/Doomsday_rule
  20. '
  21. ' SPECIAL DATES
  22. ' These are dates that when the Dooms Day Code for the year in question is calculated, these dates all have the
  23. ' same DOW.
  24. '
  25. ' The "Speical Dates" or "Refernce Dates" where the Doomsday DOW code is the same through the currenly selected
  26. ' year are:
  27. '   4/4, 6/6, 8/8, 10/10 and 12/12 --> These set of dates are referred to as "Doubles"
  28. '   5/9, 7/11, 9/5 and 11/7        --> Reverse dates
  29. '   3/14                           --> PI Day
  30. '   2/28 or 2/29                   --> Non Leap-year (2/28) or Leap-year (2/29)
  31. '   1/3 or 1/4                     --> Non Leap-year (1/3) or Leap-year (1/4)
  32. '
  33. ' All these days have the same DOW. For example, if it is 9/7/2015, the Doomsday code for 2015 is 06 or "Saturday."
  34. ' This means that all the above dates fall on Saturday in 2015 (use the non Leap-year dates).
  35. '
  36. ' To use this to find out what day-of-week 9/7/2015 falls on, find the closest day from the reference dates, and
  37. ' do a simple addition or subtraction. The date 9/7 is the closest date, and we know that 9/7 is a "Saturday" based
  38. ' on the calculated Doomsday code. We only need to subtract 5 from 7, giving us 2, then subracting 2 from 6, giving
  39. ' us 4. DOW(4) = Thurdsday. So 9/7/2015 falls on a Thursday.
  40. '
  41. ' If we want to calculate the DOW that Christmas falls on in 2015 (12/25/2015) the same proceedure is used.
  42. ' The closest reference date is 12/12. Subtracting 12 from 25 gives us 13. 13 MOD 7 = 6. Since 6 is one day short
  43. ' of a whole week, we only need to subtract one day from the Doomsday Code ((7-6)-1)=5. Christmas 2015 is on Friday.
  44. '
  45. ' This program uses this logic to calculate any DOW from any date from 1583 to well into the future. Any date before
  46. ' the Gregorian Calendar was adopted in your country will give you inaccurate results.  Check the following link:
  47. '     https://en.wikipedia.org/wiki/List_of_adoption_dates_of_the_Gregorian_calendar_per_country
  48. '
  49. '
  50. ' PROGRAM NOTES
  51. '
  52. ' This program uses both the Dooms Day algorithm and incorporates the mental math trick to calculate the Day-of-Week
  53. ' in your head. By using this method makes the code (and process of) more understandable and slightly faster than
  54. ' using the complex algorithms using the Julian Date and doing all the divisions by 4, 100 and 400 for Leap Years.
  55. '
  56. ' Below is a description of what each FUNCTION/SUBROUTINE does in this program:
  57. '
  58. ' floor          - A C++ Library that does not round up numbers when converting FLOAT to INTEGER. (Thanks to
  59. '                  both Fleppie and Spriggs for this)
  60. '
  61. ' dooms_day_code - Calculates the Doomsday Code for the century of the supplied date. This is needed as input into
  62. '                  the calculation of the actual year's Dooms Date code.
  63. '
  64. ' dooms_date     - Takes the century "anchor" code from dooms_day_code and calculates the Dooms Day code for the
  65. '                  actual year of the date.  This will be used to calculate the Day-of-Week for the date supplied.
  66. '____________________________________________________________________________________________________________________
  67.  
  68.     FUNCTION floor## (BYVAL num AS _FLOAT)
  69.  
  70. DIM SHARED dict_day$(7)
  71. DIM SHARED year, dow_anchor AS INTEGER
  72.  
  73. '*** map to store days value of anchor day can be known
  74. dict_day$(0) = "Sunday"
  75. dict_day$(1) = "Monday"
  76. dict_day$(2) = "Tuesday"
  77. dict_day$(3) = "Wednesday"
  78. dict_day$(4) = "Thursday"
  79. dict_day$(5) = "Friday"
  80. dict_day$(6) = "Saturday"
  81.  
  82.  
  83. main:
  84. '*** Enter Year for Dooms Day Code and Display results
  85.     INPUT "Enter YEAR (CCYY): "; year
  86.     IF year < 1582 OR year > 2199 THEN
  87.         PRINT "*** ERROR: Date Invalid/Out Of Range"
  88.         GOTO main
  89.     END IF
  90.  
  91. '*** Get the Doomsday Code for the anchor
  92.     dow_anchor = dooms_day_code(year)
  93.  
  94. '*** Get the Doomsday Code using the century anchor for the year's actual Doomsday code and display
  95.     PRINT "Doomsday in the year "; year; " = "; dict_day$(dooms_day(year, dow_anchor))
  96.     END
  97.  
  98. '
  99. ' *** End of Main
  100. '____________________________________________________________________________________________________________________
  101.  
  102.  
  103. '*** FUNCTIONS AND SUBROUTINES
  104. '
  105.  
  106. FUNCTION dooms_day (year, anchor AS INTEGER)
  107.  
  108. DIM yr$, cc_str$, yy_str$
  109. DIM yy, cc AS INTEGER
  110.  
  111. '*** Split out the century from the year and create two variables
  112.     yr$ = STR$(year)
  113.     yr$ = _TRIM$(yr$)
  114.     cc_str$ = LEFT$(yr$, 2)
  115.     yy_str$ = RIGHT$(yr$, 2)
  116.     yy = VAL(yy_str$)
  117.     cc = VAL(cc_str$)
  118.  
  119. '*** Dooms day formula by1 John Conway
  120.     dooms_day = (yy + floor(yy / 4) + anchor) MOD 7
  121.    
  122.  
  123.  
  124. FUNCTION dooms_day_code (year)
  125.  
  126.     DIM y, anchor, yy, cc AS INTEGER
  127.     DIM yr$, cc_str$, yy_str$
  128.  
  129. '*** Split out the century from the year and create two variables
  130.     yr$ = STR$(year)
  131.     yr$ = _TRIM$(yr$)
  132.     cc_str$ = LEFT$(yr$, 2)
  133.     yy_str$ = RIGHT$(yr$, 2)
  134.     yy = VAL(yy_str$)
  135.     cc = VAL(cc_str$)
  136.  
  137. '*** Decide the anchor day
  138.     SELECT CASE cc_str$
  139.            CASE "17"
  140.                 anchor = 0
  141.            CASE "18"
  142.                 anchor = 5
  143.            CASE "19"
  144.                 anchor = 3
  145.            CASE "20"
  146.                 anchor = 2
  147.            CASE "21"
  148.                 anchor = 0
  149.            CASE "22"
  150.                 anchor = 5
  151.     END SELECT
  152.  
  153. '*** Dooms day formula by John Conway
  154.     dooms_day_code = ((floor(y / 12) + y MOD 12 + floor(y MOD 12) / 4) MOD 7 + anchor) MOD 7
  155.  
  156. '
  157. ' *** End of Program Source
  158. '____________________________________________________________________________________________________________________
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: day of the week function
« Reply #5 on: October 22, 2021, 06:49:28 pm »
I made a Mayan Calendar app, but it got erased in 2012. :(

Hey where's Larryrl or his brother Darrelry or his other brother Darrelry, anyway?

Hey George, since "Dooms Day" falls on a Sunday in 2021, do you have another version that gives the time. I'm barbecuing this Sunday, and if necessary, I'll start the coals early.

Pete
« Last Edit: October 22, 2021, 09:10:48 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: day of the week function
« Reply #6 on: October 22, 2021, 09:00:44 pm »
Donkey Dust!

I was looking for something clever for you @Pete, but I reran my code when all you posted was that doomsday in 2021 was on a Sunday.

The code that does what is described below is missing. I lost code (how novel!) A while back I consolidated all my BASIC code is a master directory in my SourceCode folder, and it appears I overlaid this program.

It should have allowed you to input a full date, and the doomsday code is for use in quickly calculating the DOW based on "Special" dates, like this "Special" programmer who preformed a doomsday event on his source code (DOH!)

Quote
' To use this to find out what day-of-week 9/7/2015 falls on, find the closest day from the reference dates, and
' do a simple addition or subtraction. The date 9/7 is the closest date, and we know that 9/7 is a "Saturday" based
' on the calculated Doomsday code. We only need to subtract 5 from 7, giving us 2, then subracting 2 from 6, giving
' us 4. DOW(4) = Thurdsday. So 9/7/2015 falls on a Thursday.
'
' If we want to calculate the DOW that Christmas falls on in 2015 (12/25/2015) the same proceedure is used.
' The closest reference date is 12/12. Subtracting 12 from 25 gives us 13. 13 MOD 7 = 6. Since 6 is one day short
' of a whole week, we only need to subtract one day from the Doomsday Code ((7-6)-1)=5. Christmas 2015 is on Friday.

____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: day of the week function
« Reply #7 on: October 22, 2021, 09:21:20 pm »
No worries. I'm currently working on a program to calculate when Hell will freeze over. People ask me all the time, "Pete, when is it going to be finished?" I'll tell you the same thing I tell all of the rest of them. It will be finished when Hell freezes over.

What's that dear? Time for my meds again! Already? Argh.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/