# Calculating Day of the Week

#### hippy

##### Technical Support
Staff member
This calculates the day of the week from a date.

Based on Zeller's algorithm I think, made as simple as it can be and without optimisation as adding that can make it all a bit confusing and require more temporary variables. The 'dow' value ( 1 to 7 ) is compatible with the value required when using a DS1307 etc. The 'year' must be a word variable.

Code:
``````Symbol year  = w0 ; b1:b0 - Must be a word variable
Symbol month = b2 ; 1 to 12
Symbol day   = b3 ; 1 to 31
Symbol dow   = b4 ; 1=Mon .. 7=Sun (ISO8601)``````
Code:
``````Lookup month, ( 0,5,1,0,3,5,1,3,6,2,4,0,2 ), dow
If month < 3 Then
dow = year - 1 / 4 - 1 + year + day + dow % 7 + 1
Else
dow = year     / 4     + year + day + dow % 7 + 1
End If``````
If you have data which is used with a DS1307 etc where the date is specified as byte BCD. This should convert to suitable values for the algorithm ...

Code:
``````year  = ybcd / 16 * \$FA + ybcd & \$FF + 2000
month = mbcd / 16 * \$FA + mbcb
day   = dbcd / 16 * \$FA + dbcd``````
Example and test program attached.

#### Attachments

• 2 KB Views: 23
Last edited: