November 28, 2020, 11:30:15 AM

Author Topic: [bmx] Weekday of a date (BlitzMax) by wedoe [ 1+ years ago ]  (Read 825 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Weekday of a date (BlitzMax)
Author : wedoe
Posted : 1+ years ago

Description : This code will give you the correct weekday of any date between 1772 and 2099 !

Code :
Code: BlitzMax
  1. '------------------------------- DOD (Day of date)
  2. Function dod$(day,month,year) ' (DD,MM,YYYY)
  3. Local d,a,m,y,tp$
  4.  
  5. a=14-month
  6. a=a/12
  7. y=year-a
  8. m=month+(12*a)-2
  9.  
  10. d=(day+y+(y/4)-(y/100)+(y/400)+((31*m)/12)) Mod 7
  11.  
  12. Select d
  13.  Case 0 tp$="Sunday "
  14.  Case 1 tp$="Monday "
  15.  Case 2 tp$="Tuesday"
  16.  Case 3 tp$="Wednesday "
  17.  Case 4 tp$="Thursday"
  18.  Case 5 tp$="Friday "
  19.  Case 6 tp$="Saturday "
  20. End Select
  21.  
  22. Return tp$                      ' Return day
  23. End Function


Comments :


Airilsm(Posted 1+ years ago)

 Wow, very helpful addition.I believe many people don't know how that calculation work but it's work!! Very simple and short formula.thanks for sharing


n8r2k(Posted 1+ years ago)

 i dont have a clue what your doing, but why only 1772-2009?


stayne(Posted 1+ years ago)

 2099


H&K(Posted 1+ years ago)

 Thats when the world is penciled in to end.


xlsior(Posted 1+ years ago)

 <div class="quote"> I dont have a clue what your doing, but why only 1772-2009 </div>The 1772 is probably due to the switch-over from the Julian calendar to the gregorian calendar, although the exact year it was implemented varies from country to country.<div class="quote"> <a href="http://www.timeanddate.com/date/leapyear.html" target="_blank">http://www.timeanddate.com/date/leapyear.html[/url]Change from Julian to Gregorian calendarThe Julian calendar had introduced too many leap days, so that vernal equinox did no longer happen around March 21, as it did back in AD 325 during the Council of Nicaea. The introduction of the Gregorian calendar should realign the calendar with the equinox, so a number of days had to be dropped when going from the Julian to the Gregorian calendar. The links below show the calendars with the days dropped when the change to the Gregorian calendar occurred: The Gregorian calendar was first adopted in Italy, Poland, Portugal and Spain in 1582. This was done by dropping 10 days in October (Ottobre). In Great Britain (and to-become USA), the Gregorian calendar was adopted much later - 11 days were dropped in September 1752. Sweden (and Finland) had a "double" leap year in 1712 - two days were added to February, so that there was a date February 30, 1712. (This was done because the leap year in 1700 was dropped and Sweden's calendar was not synchronized with any other calendar - by adding an extra day in 1712, they were back on the Julian calendar). The old Julian calendar is currently (between year 1901-2099) 13 days past the Gregorian calendar (because too many leap years were added).  </div>Anyway, thanks to these artificial calendar corrections, these kind of formulas do not give accurate results for dates before the conversion... Unless you add a *lot* more checks & conditions to account for them.


wedoe(Posted 1+ years ago)

 <div class="quote"> ...The 1772 is probably due to the switch-over from the Julian calendar to the gregorian calendar </div> Spot on !That's when they switched from Julian to Gregorian in my Country :o)In Italy, Poland, Portugal and Spain it's good from 1552 !The formula should be good until 4099 when an adjustment of the Gregorian calendar is sceduled but I have not testedthe formula beyond 2099. (Had no need for it you see...)<div class="quote"> i dont have a clue what your doing </div> It's not very complex at all, it's the "14" and "-2" that is confusing to mostbut this is only done to make february the last month of the year in the formulaand this way the formula gets a lot more simple when leapyear just add an extra day at the end of the year, not squeezed in as day nr 60 of 366 !


H&K(Posted 1+ years ago)

 <div class="quote"> ...until 4099 when an adjustment of the Gregorian calendar is sceduled  </div>The audacity of this astounds me


wedoe(Posted 1+ years ago)

 <div class="quote"> The audacity of this astounds me  </div>He he !


Kevin_(Posted 1+ years ago)

 Wedoe is a plagiarist<a href="codearcs6a2e.html?code=997" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=997[/url]


degac(Posted 1+ years ago)

 well he missed<div class="quote"> ;Ooouch! </div>on the main formula...and to be correct...<a href="http://quasar.as.utexas.edu/BillInfo/doomsday.html" target="_blank">http://quasar.as.utexas.edu/BillInfo/doomsday.html[/url]


Rook Zimbabwe(Posted 1+ years ago)

 
Code: [Select]
; Get the day of the week example by Prof.
;
Graphics 320,240,0,2
SetBuffer BackBuffer()

dday$=Input$("What day were you born? ")
mmonth$=Input$("What month were you born? ")
yyear$=Input$("What year were you born? ")
Cls
Day$=GetDayOfTheWeek(dday$,mmonth$,yyear$) ; <- Put any date in here

Text 10,10,Day$
Flip
WaitKey()
End


Function GetDayOfTheWeek$(day,month,year)
 ; Returns the day of the week.
 ; day, month & year are integers i.e. 15 04 2004
  a=(14-month)/12
  y=year-a
  m=month+(12*a)-2
  d=(day+y+(y/4)-(y/100)+(y/400)+((31*m)/12))Mod 7  ;Ooouch!
  Select d
    Case 0:Weekday$="Sunday - bonny and blithe, and good and gay"
    Case 1:Weekday$="Monday - Fair of face"
    Case 2:Weekday$="Tuesday - Full of grace"
    Case 3:Weekday$="Wednesday - Full of woe"
    Case 4:Weekday$="Thursday - Far to go"
    Case 5:Weekday$="Friday - Loving and giving"
    Case 6:Weekday$="Saturday - Work for a living"
    Default:Weekday=""
  End Select
  Return Weekday$
End Function
Added 1 little step to make it useful... thatnks Prof and Wedoe!


Xerra(Posted 1+ years ago)

 This is an impressive bit of code. I'd shudder trying to work out a simple function to do this myself :)


wedoe(Posted 1+ years ago)

 <div class="quote"> Wedoe is a plagiarist </div> It's not exactly a secret how to do this, I just made a BM version of an old math-book example. I was not aware of your snippet though.If anyone know a formula to calculate moon-fases, 100% correctly, I'd like to know :o)


wedoe(Posted 1+ years ago)

 <div class="quote"> This is an impressive bit of code. I'd shudder trying to work out a simple function to do this myself :)  </div>I guess you refer to this:d=(day+y+(y/4)-(y/100)+(y/400)+((31*m)/12)) Mod 7It's very simple really, it's just a way to say thatevery 4th year is a leapyear, but not if it's dividableby 100, but still if it's dividable by 400.This is done to make up for the few seconds the calendarmisses by just using ever 4 year as a leapyear.Mod 7 explains itself I guess :) [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal