Page 1 of 1

Function for finding the day of any date

Posted: 11 Dec 2022, 10:14
by GameNtt
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.)

Re: Function for finding the day of any date

Posted: 13 Dec 2022, 12:07
by TheDewd
I always get the error "The Date given is invalid, please try again"

Re: Function for finding the day of any date

Posted: 13 Dec 2022, 23:16
by DataLife
I also always get "The Date given is invalid, please try again"

Is this for Autohotkey V2?

Re: Function for finding the day of any date

Posted: 14 Dec 2022, 09:38
by GameNtt
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.

Re: Function for finding the day of any date

Posted: 14 Dec 2022, 09:41
by TheDewd
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

Re: Function for finding the day of any date

Posted: 14 Dec 2022, 10:00
by GameNtt
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.

Re: Function for finding the day of any date

Posted: 14 Dec 2022, 10:19
by carno
Tested and worked great on the first run on Win 7 Pro.

Re: Function for finding the day of any date

Posted: 14 Dec 2022, 10:25
by GameNtt
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.

Re: Function for finding the day of any date

Posted: 14 Dec 2022, 18:23
by kunkel321
Pretty cool -- Thanks for sharing!

Re: Function for finding the day of any date

Posted: 15 Dec 2022, 05:48
by ozzii
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

Re: Function for finding the day of any date

Posted: 17 Dec 2022, 01:09
by GameNtt
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.

Re: Function for finding the day of any date

Posted: 17 Dec 2022, 03:38
by GameNtt
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.

Re: Function for finding the day of any date

Posted: 02 Sep 2023, 22:13
by carno
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
    }
}