Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 23:38 02 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : DAY OF WEEK ERRORS

Author Message
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 904
Posted: 01:19am 09 Apr 2024
Copy link to clipboard 
Print this post

I have copied a formula from the web to get the day of week from the date. The code is giving me a zero response (Sunday) can anyone see where I have made the error?


Sub  GetDayOfWeek
 D=Val(Left$(Date$,2))
 M=Val(Mid$(Date$,4,2))
 C=Val(Mid$(Date$,7,2))
 Y=Val(Mid$(Date$,9,2))
 W=(D+(2.6*M-0.2)-2*C+Y+Y/4+C/4) Mod 7
Return


0A47
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1804
Posted: 01:36am 09 Apr 2024
Copy link to clipboard 
Print this post

Have a look in Fruit of the Shed there is a function in there for it.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
morgs67
Regular Member

Joined: 10/07/2019
Location: Australia
Posts: 71
Posted: 03:01am 09 Apr 2024
Copy link to clipboard 
Print this post

This is what I use- (could be a bit wordy!)

'Tomohiko Sakamoto's Algorithm - lookup table
Dim integer MonthLookup(11) = (0,3,2,5,0,3,5,1,4,6,2,4)
Dim MonthNames$(11) = ("January","February","March","April","May","June","July","August","September","October","November","December")
'Tomohiko Sakamoto's Algorithm - dayname lookup table
Dim DayNames$(6) = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")


' Tomohiko Sakamoto's Algorithm- Finding the day of the week
Function DayOfWeek$(d,m,y)
 Local dow = 0
 LOCAL am = m
 LOCAL ay = y
 
 'if month is less than 3 reduce year by 1  
 if am<3 then ay = ay-1  
 dow = (ay + (ay\4) - (ay\100) + (ay\400) + MonthLookup(am-1)+d)MOD 7    
 DayOfWeek$ = DayNames$(dow)
End Function

Good for the Georgian calendar.

Tony

Footnote added 2024-04-09 13:20 by morgs67
@palcal Would that be Gregorian.  -ooops spellcheck
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1804
Posted: 03:14am 09 Apr 2024
Copy link to clipboard 
Print this post

  Quote  Good for the Georgian calendar.

Would that be Gregorian.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 1790
Posted: 03:21am 09 Apr 2024
Copy link to clipboard 
Print this post

Depending on the version of MMBasic this may be a little shorter:-
> date$="09-04-2024"
> ? DAY$(date$)
Tuesday
>


@OA47 in your first post see if changing "Return" to "End Sub" makes any difference.

Edit. No, just the same.
  Quote  > GetDayOfWeek : ? date$,D,M,Y,W
09-04-2024       9       4       24      0
>

Edited 2024-04-09 13:35 by phil99
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 904
Posted: 03:52am 09 Apr 2024
Copy link to clipboard 
Print this post

Thanks Phil, I had not caught up with DAY$(DATE$) and that works perfectly as I am using mm.ver=5.070725 in this project.

Changing RETURN to End Sub did not change the result, it must have been an old programmers Freudian slip.  

0A47
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 1790
Posted: 04:00am 09 Apr 2024
Copy link to clipboard 
Print this post

With a minor change the Sub seems to work.
>date$="01-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
01-04-2024       1       4       24      1      Monday
> date$="02-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
02-04-2024       2       4       24      2      Tuesday
> date$="03-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
03-04-2024       3       4       24      3      Wednesday
> date$="04-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
04-04-2024       4       4       24      4      Thursday
> date$="05-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
05-04-2024       5       4       24      5      Friday
> date$="06-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
06-04-2024       6       4       24      6      Saturday
> date$="07-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
07-04-2024       7       4       24      0      Sunday
> date$="08-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
08-04-2024       8       4       24      1      Monday
> date$="09-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
09-04-2024       9       4       24      2      Tuesday
> date$="10-04-2024" : GetDayOfWeek : ? date$,D,M,Y,W, DAY$(date$)
10-04-2024       10      4       24      3      Wednesday
> LIST
Sub  GetDayOfWeek
D=Val(Left$(Date$,2))
M=Val(Mid$(Date$,4,2))
C=Val(Mid$(Date$,7,2))
Y=Val(Mid$(Date$,9,2))
W=(D+(2.6*M-0.2)-2*C+Y+Y/4+C/4 +2) Mod 7
End Sub
>
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 904
Posted: 04:22am 09 Apr 2024
Copy link to clipboard 
Print this post

Phil, I am not sure why the addition of 2 makes it work but well done.
0A47
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 1790
Posted: 04:45am 09 Apr 2024
Copy link to clipboard 
Print this post

By chance it happened to produce 0 on Tuesday and 1 om Wednesday so added an offset.
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1328
Posted: 05:18am 09 Apr 2024
Copy link to clipboard 
Print this post

Here's one from "Microblocks" who was a quite prolific and very capable TBS member for a number of years. Unfortunately he is no longer with us.
It looks a bit long but there's a lot of padding there that can be shortened.

Sub dayndate        'day-of-week code thanks to "MicroBlocks" at TBS forum.
 Local integer year,month,day,dayofweek,a,m,y
 Local string DOW$(6) length 3 = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
 Local string dispdayndate$
 curdate$ = Date$
 Year=Val(Mid$(curdate$,7,4))
 Month=Val(Mid$(curdate$,4,2))
 Day=Val(Mid$(curdate$,1,2))
 a = Int((14-month)/12)
 m = month + 12*a - 2
 y = year - a
 DayOfWeek = (day+y+Int(y/4)-Int(y/100)+Int(y/400)+Int(31*m/12))Mod 7
 dispdayndate$ = DOW$(DayOfWeek)+" "+curdate$
 Text MM.HRes/2,MM.VRes-MM.Info(FONTHEIGHT),dispdayndate$,CB
End Sub

Greg
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 904
Posted: 05:26am 09 Apr 2024
Copy link to clipboard 
Print this post

Greg, years ago I was in regular contact with Microblocks we had a good trade happening, I would supply him with electronic magazines and he could source the 28 pin micromites from just around the corner from where he lived. IIRC his christian name was Jean.

0A47
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 1790
Posted: 05:37am 09 Apr 2024
Copy link to clipboard 
Print this post

This one:- W=(D+(2.6*M-0.2)-2*C+Y+Y/4+C/4 +2) Mod 7
ignores the century and 4 century corrections.

This one:- DayOfWeek = (day+y+Int(y/4)-Int(y/100)+Int(y/400)+Int(31*m/12))Mod 7
ignores the 16 century correction next due in 3200.
Keep an eye out for that ;-)
 
Quazee137

Guru

Joined: 07/08/2016
Location: United States
Posts: 523
Posted: 08:14am 09 Apr 2024
Copy link to clipboard 
Print this post

Here's what I've used for some time. Works going forward in time.

Function dow(d As Integer,m As Integer,y As Integer) As Integer

'  1       2       3       4       5       6       7
' sun     mon     tue     wed     the     fri     sat

  If m < 3 Then
     m=m + 12
     y=y-1
  End If
  dow = 1 + (d + ((26 * m+1)/10) - (2 * (y\100)) + y + (y\4) + (y\400) ) Mod 7
End Function
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3551
Posted: 02:10pm 09 Apr 2024
Copy link to clipboard 
Print this post

As hinted before:

print day$(date$)


Volhout
PicomiteVGA PETSCII ROBOTS
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1328
Posted: 02:59am 10 Apr 2024
Copy link to clipboard 
Print this post

  OA47 said  Greg, years ago I was in regular contact with Microblocks we had a good trade happening, I would supply him with electronic magazines and he could source the 28 pin micromites from just around the corner from where he lived. IIRC his christian name was Jean.

Graeme - yes I knew he was Jean, and he was pretty young when he passed away. He helped me with some programming stuff and helped a lot more Thai students with his electronics school where he was living in Thailand.
Greg
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 904
Posted: 05:08am 10 Apr 2024
Copy link to clipboard 
Print this post

  Quote  Graeme - yes I knew he was Jean, and he was pretty young when he passed away. He helped me with some programming stuff and helped a lot more Thai students with his electronics school where he was living in Thailand.
Greg


It seems his Microblcks username is still registered in the member list but his original TZadvantage no longer exists.

0A47
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024