​ ​ ​ ​ Calculating Day of the Week
Results 1 to 1 of 1

Thread: Calculating Day of the Week

  1. #1
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    22,857

    Default Calculating Day of the Week

    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.
    Attached Files Attached Files
    Last edited by hippy; 19-12-2016 at 19:59.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •