Get the weekday from a date

Blitz3D Forums/Blitz3D Programming/Get the weekday from a date

GeordieB(Posted 2004) [#1]
Anyone got any idea how to approach writing a function to quickly get/work out the weekday of any date?

i need this badly for something im working on, written tons of other date functions for getting any other info, but this i just cant work out, least not doing it quickly anyway

Thanks


Eric(Posted 2004) [#2]
Don't know if this works but

http://www.anvari.org/fun/Quizzes_and_Tests/What_Day_of_the_Week.html

To calculate the day on which a particular date falls, the following
algorithm may be used (the divisions are integer divisions, in which
remainders are discarded; % means all we want is the remainder):

a = (14 - month) / 12
y = year - a
m = month + 12*a - 2
For Julian calendar: d = (5 + day + y + y/4 + (31*m)/12) % 7
For Gregorian calendar: d = (day + y + y/4 - y/100 + y/400 + (31*m)/12) % 7

The value of d is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc.


GeordieB(Posted 2004) [#3]
woot, cheers for that, exactly what i was after, thanks Eric


soja(Posted 2004) [#4]
If you need something more robust or wish to use built-in Windows commands, here's an example:
;.lib "Kernel32.dll"
;GetDate%(iLocale%, iFlags%, iDate%, iFormat$, oDate$, iSizeBuf%):"GetDateFormatA"
;GetDateAny%(locale%, dwFlags%, lpDate*, lpFormat$, lpDateStr$, size%):"GetDateFormatA"

day$ = String(" ", 150)
GetDate(0, 0, 0, "d dd ddd dddd M MM MMM MMMM y yy yyyy gg", day, Len(day))
Print day

time = CreateBank(16)
PokeShort(time, 0, 2004) ; Year
PokeShort(time, 2, 4) ; Month
PokeShort(time, 6, 17) ; Day
GetDateAny(0, 0, time, "ddd MM-dd-yy", day, Len(day))
Print day



BODYPRINT(Posted 2004) [#5]
soja:

I don't get any print from the variable "day"???
I have made the kernel32 decls file and the program runs fine, just no printout?

Any ideas


soja(Posted 2004) [#6]
Oh yeah, I cooked this up in BlitzPlus way back when and I quickly grabbed the sample without realizing that it wouldn't work the same in Blitz3D.

Blitz3D doesn't handle strings passed to/from userlibs the same way BlitzPlus does.

What you have to do is something like this:
Change...
day$ = String(" ", 150)
...to...
day = Createbank(150)
...and:
...Len(day)
...to...
...BankSize(day)
...and then you would have to use PeekByte to get the text out of the bank before you print it. Yeah, it's more of a hassle. I'm not sure why Blitz3D doesn't work "quite right"...


soja(Posted 2004) [#7]
Oh, and I forgot to say to change the userlib declaration of date$ to date*.

Here's and example that works in B3D (with a generic bank to string function):
;.lib "Kernel32.dll"
;GetDate%(iLocale%, iFlags%, iDate%, iFormat$, oDate*, iSizeBuf%):"GetDateFormatA"
;GetDateAny%(locale%, dwFlags%, lpDate*, lpFormat$, lpDateStr*, size%):"GetDateFormatA"

day = CreateBank(150)
GetDate(0, 0, 0, "d dd ddd dddd M MM MMM MMMM y yy yyyy gg", day, BankSize(day))
Print Out(day)

time = CreateBank(16)
PokeShort(time, 0, 2004) ; Year
PokeShort(time, 2, 4) ; Month
PokeShort(time, 6, 17) ; Day
GetDateAny(0, 0, time, "ddd MM-dd-yy", day, BankSize(day))
Print Out(day)


Function Out$(b)
	For i = 0 To BankSize(b)-1
		a=PeekByte(b,i)
		If a=0 Then Exit
		s$=s$+Chr(a)
	Next
	Return s$
End Function



Difference(Posted 2004) [#8]
Is this not what you are looking for? : http://www.blitzbasic.com/codearcs/codearcs.php?code=298


BODYPRINT(Posted 2004) [#9]
For extra info, if you add "ww" in the date format you will get the week number in the year.
This matches weeks in your Yearly Diary/Personel Planner books.

I am getting memory access violations now with this version, but will persist till I get it working :-)


soja(Posted 2004) [#10]
Make sure your banks are big enough, and that they're declared as *