[bmx] Weekday of a date (BlitzMax) by wedoe [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:38

Previous topic - Next topic

BlitzBot

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) Select
'------------------------------- DOD (Day of date)
Function dod$(day,month,year) ' (DD,MM,YYYY)
Local d,a,m,y,tp$

a=14-month
a=a/12
y=year-a
m=month+(12*a)-2

d=(day+y+(y/4)-(y/100)+(y/400)+((31*m)/12)) Mod 7

Select d
 Case 0 tp$="Sunday "
 Case 1 tp$="Monday "
 Case 2 tp$="Tuesday"
 Case 3 tp$="Wednesday "
 Case 4 tp$="Thursday"
 Case 5 tp$="Friday "
 Case 6 tp$="Saturday "
End Select

Return tp$ ' Return day
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</a>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</a>


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</a>


Rook Zimbabwe(Posted 1+ years ago)

 
; 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]