--- title: "Response To Day_Of_Week" type: "article" slug: "response-to-day_of_week" url: "http://localhost/article/response-to-day_of_week/" markdown_url: "http://localhost/article/response-to-day_of_week.md" published_at: "2025-12-18T18:29:31+00:00" modified_at: "2026-06-21T23:36:29+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "While playing around with the function on paper there appears to be a slight error. I am sure that I will not be the first to see this but I will attempt to explain anyway. The year part of the function Y + Y/4 - Y/100 + Y/400 is correct taking into account leap years,…" category: - name: "QL Hacker's Journal" slug: "ql-hackers-journal" taxonomy: "category" url: "http://localhost/category/periodicals/ql-hackers-journal/" post_tag: - name: "Full Text" slug: "fulltext" taxonomy: "post_tag" url: "http://localhost/tag/fulltext/" - name: "QL" slug: "ql" taxonomy: "post_tag" url: "http://localhost/tag/ql/" - name: "Superbasic (programming language)" slug: "superbasic-programming-language" taxonomy: "post_tag" url: "http://localhost/tag/superbasic-programming-language/" model: - name: "Sinclair QL" slug: "sinclair-ql" taxonomy: "model" url: "http://localhost/model/sinclair-ql/" publication_r: id: 33686 title: "QL Hacker’s Journal" type: "periodical" url: "http://localhost/periodical/ql-hackers-journal/" authors: "John Southern" issue: "23" issues_articles: - id: 61559 title: "QL Hacker’s Journal 23" type: "issue" url: "http://localhost/issue/ql-hackers-journal-23/" pubdate: "January 1996" archive_link: true --- # Response To Day_Of_Week While playing around with the function on paper there appears to be a slight error. I am sure that I will not be the first to see this but I will attempt to explain anyway. The year part of the function Y + Y/4 – Y/100 + Y/400 is correct taking into account leap years, centuries and leap centuries. The month part however, 2*m + 3*(m+1)/5, is fine in most cases except for the way rounding occurs. Consider 20 June 1995. We get: ``` 30+2*6+3*7/5+1995+1995/4-1995/100+1995/400 30+12+4.2+1995+498.75-19.95+4.9875 2524.9875 ``` The Modulus 7 of this is 4.9875. Rounded up to give 5, which equals Friday. Now consider 2 July 1995. We get: ``` 2+2*7+3*8/5+1995+1995/4-1995/100+1995/400 2+14+4.8+1995+498.75+4.9875 2499.5875 ``` The Modulus 7 of this is 0.5875. Rounded up this give us 1, which equals Monday. This should be 0 for Sunday. To correct this could I suggest the following function instead: ``` DEFine FuNction day_of_week$(d,m,y) LOCal a IF m=1 OR m=2 THEN m=m+12 d=d-1 END IF a = INT((d+2.6*m+y+y/4-y/100+y/400+0.8125) MOD 7) IF a = 0 THEN RETurn "Sunday" IF a = 1 THEN RETurn "Monday" IF a = 2 THEN RETurn "Tuesday" IF a = 3 THEN RETurn "Wednesday" IF a = 4 THEN RETurn "Thursday" IF a = 5 THEN RETurn "Friday" IF a = 6 THEN RETurn "Saturday" END DEFine ``` The 2.6*m is for the offset each month required due to months not being made up of an exact number of weeks. The 0.8125 is the initial adjustment to set Sundays correctly.