Function for finding the day of any date

Post your working scripts, libraries and tools for AHK v1.1 and older
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Function for finding the day of any date

11 Dec 2022, 10:14

I've made a function for getting the day of the week of any date in the Gregorian Calendar. It's based off of a variation of the Zeller's Congruence https://en.wikipedia.org/wiki/Zeller%27s_congruence and because of the the final result is one more, 0 is taken as Sunday. If anyone has any suggestions for improvement, you are welcome to give feedback.

Code: Select all

;GameNtt
Gui, New, ,A Program To Find The Day Of Any Date
Gui, Add, Text, ,Example Date Is 24/09/2022, Where 24 Is The Date, 09 Is The Month, 2022 Is The Year 
Gui, Add, Text, ,Enter The Date
Gui, Add, Edit, vDate
Gui, Add, Text, ,Enter The Month
Gui, Add, Edit, vOMonth
Gui, Add, Text, ,Enter The Year In Atleast Four Digits 
Gui, Add, Edit, vOYear
Gui, Add, Button, default xm, OK
Gui, Show
Return

ButtonOK:
Gui, Submit
if(OMonth == 1)
{
    Month := 11
    Year := OYear-1
}
if(OMonth == 2)
{
    Month := 12
    Year := OYear-1
}
if(OMonth != 1 && OMonth != 2)
{
    Month := OMonth-2
    Year := OYear    
}
YearLen := StrLen(Year)
Decade := SubStr(Year, -1, 2)
Century := SubStr(Year, 1, YearLen-2)
F := Date + (((13*Month)-1)//5) + Decade + (Decade//4) + (Century//4) - (2*Century)
modF := cMod(F,7)
If (modF == 0)
ans := "Sunday"
Else If (modF == 1)
ans := "Monday"
Else If (modF == 2)
ans := "Tuesday"
Else If (modF == 3)
ans := "Wednesday"
Else If (modF == 4)
ans := "Thursday"
Else If (modF == 5)
ans := "Friday"
Else If (modF == 6)
ans := "Saturday"
Else
ans := "invalid"
Gui, New, ,A Program To Find The Day Of Any Date 
if(ans == "invalid")
{
    Gui, Add, Text, ,The Date Given Is Invalid, Please Try Again :(
    Gui, Add, Button, default,Try Again
    Gui, Add, Button, ,Exit
    Gui, Show
}
Gui, Add, Text, ,%Date%/%OMonth%/%OYear% is a %ans%
Gui, Add, Button, default,Another Date
Gui, Add, Button, ,Exit
Gui, Show
Return

ButtonTryAgain:
Reload
Return

ButtonAnotherDate:
Reload
Return

ButtonExit:
ExitApp

GuiClose:
ExitApp

cMod(dividend, divisor)
{
    if(dividend > 0)
    {
        Return Mod(dividend, divisor)
    }
    else if(divident < 0)
    {
        While(dividend < 0)
        {
            dividend += divisor
        }
        return Mod(dividend, divisor)
    }
    else
    {
        Return 0
    }
}
Edit 1 - Changed if(OMonth != 1 && !=2) to if(OMonth !=1 && OMonth!=2) and added a line Year := OYear in the aforementioned if condition which combined caused the errors mentioned by the other users.
Edit 2 - Added a comment to say that you can change the Gui button to default if you prefer it that way.
Edit 3 - Added a function cMod in case the value of F is negative which is possible due to certain century values (eg: 99) and made this Script support more than 4 digit years. (I have no idea how you would verify that far into the future but go ahead trying to verify.)
Last edited by GameNtt on 17 Dec 2022, 04:00, edited 5 times in total.
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Function for finding the day of any date

13 Dec 2022, 12:07

I always get the error "The Date given is invalid, please try again"
User avatar
DataLife
Posts: 456
Joined: 29 Sep 2013, 19:52

Re: Function for finding the day of any date

13 Dec 2022, 23:16

I also always get "The Date given is invalid, please try again"

Is this for Autohotkey V2?
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Function for finding the day of any date

14 Dec 2022, 09:38

No, this is not for Ahk V2. Could the both of you send me screenshots of all the steps you do after which you encounter this error? You could sent it via imgur in this topic.
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Function for finding the day of any date

14 Dec 2022, 09:41

GameNtt wrote:
14 Dec 2022, 09:38
No, this is not for Ahk V2. Could the both of you send me screenshots of all the steps you do after which you encounter this error? You could sent it via imgur in this topic.
1) Save script.
2) Run script.
3) Enter example date (24/09/2022)
4) Click OK
5) Error

Image
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Function for finding the day of any date

14 Dec 2022, 10:00

Yes, I had made a mistake in the if condition, I've updated the code in the first post along with the changes made. It was an oversight related to not needing to use the var name again in the same if conditon.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Function for finding the day of any date

14 Dec 2022, 10:19

Tested and worked great on the first run on Win 7 Pro.
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Function for finding the day of any date

14 Dec 2022, 10:25

If anyone finds an error (mismatch between the actual day and the day given by my program), please do inform me about that along with the date which you used to get the error.
User avatar
kunkel321
Posts: 1061
Joined: 30 Nov 2015, 21:19

Re: Function for finding the day of any date

14 Dec 2022, 18:23

Pretty cool -- Thanks for sharing!
ste(phen|ve) kunkel
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: Function for finding the day of any date

15 Dec 2022, 05:48

working now.
I will maybe just add the OK as a default key. so that we can use the enter key for validating the gui
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Function for finding the day of any date

17 Dec 2022, 01:09

Yes, that is a good idea @ozzii. I had changed it from default as I tended to send enter after every input which caused it to give an invalid date result. I'll add a comment to that saying that you can change the button to default if you prefer to submit by enter key.
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Function for finding the day of any date

17 Dec 2022, 03:38

I just realised a major flaw in this program and I have corrected it in the first post along with a comment explaining why this is such a major flaw and why this flew under my radar. I also updated it with the ability for more than 4 digit years and removed the necessity for 2 digits in the date and the month which was not actually necessary.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Function for finding the day of any date

02 Sep 2023, 22:13

I'm a tech writer and I revised some text:

Code: Select all

; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=111429

#NoEnv
#SingleInstance Force

Gui, New, ,A Program to Find the Day of Any Date
Gui, Add, Text, ,Example: 24/09/2022 Where 24 Is the Day, 09 Is the Month, 2022 Is the Year 
Gui, Add, Text, ,Enter the Day in 2 Digits 
Gui, Add, Edit, vDate
Gui, Add, Text, ,Enter the Month in 2 Digits 
Gui, Add, Edit, vOMonth
Gui, Add, Text, ,Enter the Year in 4 Digits 
Gui, Add, Edit, vOYear
Gui, Add, Button, default xm, OK
Gui, Show
Return

ButtonOK:
Gui, Submit
if(OMonth == 1)
{
    Month := 11
    Year := OYear-1
}
if(OMonth == 2)
{
    Month := 12
    Year := OYear-1
}
if(OMonth != 1 && OMonth != 2)
{
    Month := OMonth-2
    Year := OYear    
}
YearLen := StrLen(Year)
Decade := SubStr(Year, -1, 2)
Century := SubStr(Year, 1, YearLen-2)
F := Date + (((13*Month)-1)//5) + Decade + (Decade//4) + (Century//4) - (2*Century)
modF := cMod(F,7)
If (modF == 0)
ans := "Sunday"
Else If (modF == 1)
ans := "Monday"
Else If (modF == 2)
ans := "Tuesday"
Else If (modF == 3)
ans := "Wednesday"
Else If (modF == 4)
ans := "Thursday"
Else If (modF == 5)
ans := "Friday"
Else If (modF == 6)
ans := "Saturday"
Else
ans := "invalid"
Gui, New, ,A Program To Find The Day Of Any Date 
if(ans == "invalid")
{
    Gui, Add, Text, ,The Date Given Is Invalid, Please Try Again :(
    Gui, Add, Button, default,Try Again
    Gui, Add, Button, ,Exit
    Gui, Show
}
Gui, Add, Text, ,%Date%/%OMonth%/%OYear% Is %ans%
Gui, Add, Button, default,Another Date
Gui, Add, Button, ,Exit
Gui, Show
Return

ButtonTryAgain:
Reload
Return

ButtonAnotherDate:
Reload
Return

ButtonExit:
ExitApp

GuiClose:
ExitApp

cMod(dividend, divisor)
{
    if(dividend > 0)
    {
        Return Mod(dividend, divisor)
    }
    else if(divident < 0)
    {
        While(dividend < 0)
        {
            dividend += divisor
        }
        return Mod(dividend, divisor)
    }
    else
    {
        Return 0
    }
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 171 guests