# Weekday - Day of week

## Recommended Posts

I missed a function in AutoIt to get the week day (as a number from 0-6) of any date, not just of the actual date. This problem is not a simple as it first seems and I found a formula of a genious man who published this formula in the internet: http://users.aol.com/s6sj7gt/mikecal.htm .

It's incredibly simple:

dayofweek = ( [23m/9] + d + 4 + y + [z/4] - [z/100] + [z/400] - 2 (if m >= 3) ) mod 7

where

[x] means to truncate downward to the nearest integer, and

z = y - 1 if m < 3,

z = y otherwise.

Here is the AutiIt script for it:

Func Weekday(\$date)

;\$date is dd.mm.yyyy (as usual in Germany, change it as you need it

\$x = StringSplit(\$date, ".")

\$z = Number(\$x[3])

If \$x[2] < 3 then \$z = \$x[3] - 1

\$k = 0

If \$x[2] >= 3 then \$k = 2

\$Weekday = Mod(Int(23* \$x[2] / 9) + \$x[1] + 4 + \$x[3] + Int(\$z / 4) - Int (\$z/100) + Int(\$z/400) - \$k,7)

Return \$Weekday

EndFunc

0=sunday...6=saturday, If you prefer 1-7, just add 1 to the result

Try it out - it really works !

If you wish to get the day in letters, use:

Func DayOfWeek(\$Weekday)

Select

Case \$Weekday = 0

\$DayOfWeek = "So"

Case \$Weekday = 1

\$DayOfWeek = "Mo"

Case \$Weekday = 2

\$DayOfWeek = "Tu"

Case \$Weekday = 3

\$DayOfWeek = "We"

Case \$Weekday = 4

\$DayOfWeek = "Th"

Case \$Weekday = 5

\$DayOfWeek = "Fr"

Case \$Weekday = 6

\$DayOfWeek = "Sa"

EndSelect

Return \$DayOfWeek

Call the function for the day in letters: DayOfWeek(weekday("01.01.1912"))

EndFunc

Edited by pegasus912

##### Share on other sites

```Func DayOfWeek(\$Weekday)
If (\$Weekday < 0) OR (\$Weekday > 6) Then Return "Error..."
Local \$DayOfWeek = StringSplit("So-Mo-Tu-We-Th-Fr-Sa" , "-")
Return \$DayOfWeek[\$Weekday + 1]
EndFunc```

And some test code...

```For \$l = 0 to 7
MsgBox(0,"test",DayOfWeek(\$l))
Next```

##### Share on other sites

This function is also available in the http://www.autoitscript.com/fileman/users/jdeb/test/datenew.au3 on my AutoIt Stuff Page.

This file will replace the date.au3 in the UDF library when thats finished.

;===============================================================================

;

; Description:      Returns the DayofWeek number for a given Date.  0=Sunday

; Parameter(s):    \$Year

;                  \$Month

;                  \$day

; Requirement(s):  None

; Return Value(s):  On Success - Returns Day of the Week

;                  On Failure - 0 and sets @ERROR = 1

; Author(s):        Jos van der Zande <jvdZande@yahoo.com>

; Note(s):          None

;

;===============================================================================

SciTE4AutoIt3 Full installer Download page   - Beta files

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

This function is also available in the http://www.autoitscript.com/fileman/users/jdeb/test/datenew.au3 on my AutoIt Stuff Page.

This file will replace the date.au3 in the UDF library when thats finished.

<{POST_SNAPBACK}>

Your datenew - collection is great and really enhances AutoIt !

Sorry for sending the topic - I could not find anything when searching the forum with "weekday" or "day of week". I'm a Newbie with AutoIt and did not know about the user pages.

Roy

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...