Shop OBEX P1 Docs P2 Docs Learn Events
Calculating "Day Of The Week" from a date — Parallax Forums

Calculating "Day Of The Week" from a date

hmlittle59hmlittle59 Posts: 404
edited 2009-07-20 22:08 in General Discussion
Hello All,

I did a search and could not find any CODE on this forum. Does anyone know of any PBASIC code (BS2xx) that can do this? I've down loaded the Zeller's info from Google and working on my own. Thanks.

I'll search Google for some BASIC Code.

thanks again

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I'M STILL LEARNING SO MUCH...BUT STILL KNOW SO LITTLE!!!

hmlittle59

Comments

  • CounterRotatingPropsCounterRotatingProps Posts: 1,132
    edited 2009-07-17 23:04
    This is probably a good coding exercise - try it and if you can't get it to work, post what you have and we can help you figure it out. How far forward and backward in time does it have to go? - H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • mctriviamctrivia Posts: 3,772
    edited 2009-07-17 23:12
    en.wikipedia.org/wiki/Calculating_the_day_of_the_week#An_algorithm_to_calculate_the_day_of_the_week

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    propmod_us and propmod_1x1 are in stock. Only $30. PCB available for $5

    Want to make projects and have Gadget Gangster sell them for you? propmod-us_ps_sd and propmod-1x1 are now available for use in your Gadget Gangster Projects.

    Need to upload large images or movies for use in the forum. you can do so at uploader.propmodule.com for free.
  • xanatosxanatos Posts: 1,120
    edited 2009-07-17 23:20
    I have that coded in VBScript if you're interested.· I did it for my calendar manager application I wrote a few years ago.· If you can read VBS, you could probably do the same in PBASIC.· The calculations are pretty basic (no pun intended)....· I got this from the math in an OLD magic/mentalism book, believe it or not.· In "another life" I was a magician and mentalist...·· I hope the code is useful. the variable diffYear is just the 4 digit year to use (2009, 1020, etc).

       ' Here is where we calculate the day of the week from the date 
        ' (Thanks to Corrinda's "13 Steps to Mentalism"!)
       
        Dim whichDay, toChop
       
        whichDay = mid(diffYear,3,2) + int((mid(diffYear,3,2)) / 4) + monthCode + thisDate
        toChop = int(whichDay / 7)
        chopFrom = whichDay / 7
        rawDay = (chopFrom - toChop) * 7
        roundDay = Cint(rawDay)
        whichDay = roundDay - 3 ' -3 = Century Code until 2099 for comp...
        
        if diffMonth < 3 then whichDay = whichDay - leapYear ' Fudge Factor- why dates don't line up?
        
        if whichDay < 0 then whichDay = whichDay + 7
        if whichDay = 0 then
         whichDay = "Saturday"
        elseif whichDay = 1 then
         whichDay = "Sunday"
        elseif whichDay = 2 then
         whichDay = "Monday"
        elseif whichDay = 3 then
         whichDay = "Tuesday"
        elseif whichDay = 4 then
         whichDay = "Wednesday"
        elseif whichDay = 5 then
         whichDay = "Thursday"
        elseif whichDay = 6 then
         whichDay = "Friday"
        end if
     
    
  • mctriviamctrivia Posts: 3,772
    edited 2009-07-17 23:58
    spin code *untested
    should be accurate to 2099

    DAT
    MTable       long      0, 2678400,5097600,7776000,10368000,13046400,15638400,18316800,20995200,23587200,26265600,28857600
      
    
    PUB datetonum(year,month,day,hour,minute,sec) | time 'year 2 digits, month 0-11, day 0-30, hour 0-23, min 0-59, sec 0-59
      time:=31536000*year+86400*(year/4)+Long[noparse][[/noparse]@MTable][noparse][[/noparse]month]+86400*day+hour*3600+minute*60+sec
      if (year//4) & (month>1)
        time+=86400
      return time
    
    PUB dayofweek(num) | time 'sun=0, mon=1...
      time:=(6+(num/86400))//7
      return time
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    propmod_us and propmod_1x1 are in stock. Only $30. PCB available for $5

    Want to make projects and have Gadget Gangster sell them for you? propmod-us_ps_sd and propmod-1x1 are now available for use in your Gadget Gangster Projects.

    Need to upload large images or movies for use in the forum. you can do so at uploader.propmodule.com for free.
  • hmlittle59hmlittle59 Posts: 404
    edited 2009-07-18 00:38
    Hello All,

    Thanks to all that replied, I'm already using the DS1302 and wanted to CODE it in. I looked in the Editor Help file and found the (//) command for Modulus. I'll be using the examples and figure it out...Thanks again


    Hmlittle59

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I'M STILL LEARNING SO MUCH...BUT STILL KNOW SO LITTLE!!!

    hmlittle59
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-07-18 13:07
    'dy is 1..31
    'mn is 1..12
    'yr is >= 1900
    IF mn < 3 then· 'Jan & Feb = 13 & 14 preceding year
    · mn = mn + 12
    · yr = yr - 1
    ENDIF
    · weekday = (dy + 2*mn + (3*(mn+1)/5) + yr + (yr/4) - (yr/100) + (yr/400) + 1) // 7· '0=sunday


    regards peter
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-07-18 18:35
    Hmlittle,

    The DS1302 already keeps track of the day of the week. Was there something outside of that you were trying to do?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • hmlittle59hmlittle59 Posts: 404
    edited 2009-07-18 20:08
    Hello Chris,

    I just wanted to learn to do it within my program without knowing?

    Thanks Peter and to All

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I'M STILL LEARNING SO MUCH...BUT STILL KNOW SO LITTLE!!!

    hmlittle59

    Post Edited (hmlittle59) : 7/18/2009 8:34:58 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-07-18 20:51
    Ok, that's cool. I was just curious. =)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • mctriviamctrivia Posts: 3,772
    edited 2009-07-18 23:49
    i wish there was a RTC that used EPOCH time(or my equivalent starting jan 1st, 2000)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    propmod_us and propmod_1x1 are in stock. Only $30. PCB available for $5

    Want to make projects and have Gadget Gangster sell them for you? propmod-us_ps_sd and propmod-1x1 are now available for use in your Gadget Gangster Projects.

    Need to upload large images or movies for use in the forum. you can do so at uploader.propmodule.com for free.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-07-19 20:43
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2009-07-20 00:14
    There is PBASIC code posted at this link on JulianDateTime.

    RTC chips do keep a cyclic count of 7 days, but you have to initialize it to the day of week scheme you want linked to the calendar day.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • hmlittle59hmlittle59 Posts: 404
    edited 2009-07-20 22:04
    Hello Peter,
    I tried this code and I seem to be having some problems getting it to work correctly, could you help me get it working please?
    thanks to all that can help

    '   {$STAMP BS2e}
    '   {$PBASIC 2.5}
    ' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
      DOW                VAR     Byte
      Month               VAR     Byte
      Day                  VAR     Byte
      Year                 VAR     Word
    '***********************************************************************************
    '                  Set Default Month/Date/Year/Day/Time..
    '***********************************************************************************
    
      Day               =   20            ' 01st....31st
      Month            =   07            ' Jan..to..Dec
      Year              =   09            ' 01...99
    
     IF month < 3 THEN
      month =  month + 12
      year =  year - 1
     ENDIF
    
    DOW = (Day+2*month+(3*(month +1)/5)+year+(year/4)-(year/100)+(year/400) + 1) // 7
    
    DEBUG "Date of Today   ", DEC2 day  ,CR
    DEBUG "Month of Year   ", DEC2 Month,CR
    DEBUG "What's the Year ", DEC2 year ,CR
    DEBUG "Day of Week     ", DEC2 DOW  ,CR
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I'M STILL LEARNING SO MUCH...BUT STILL KNOW SO LITTLE!!!

    hmlittle59
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-07-20 22:08
    A note on Tracy's post for anyone not experienced with the DS1302. It does assign a number to the day of the week, but unlike the other registers it doesn't count 0 as a valid day, only 1-7.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
Sign In or Register to comment.