GetWeekday

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

GetWeekday

15 Apr 2014, 08:58

GetWeekday

Get the weekday on any given date
e.g. "1990-01-01" --> "Monday"

Source: GitHub

Code: Select all

GetWeekday(month, day, year)
{
    static WeekDay := ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    d := day, m := month, y := year
    if (m < 3)
    {
        m += 12
        y -= 1
    }
    wd := mod(d + (2 * m) + floor(6 * (m + 1) / 10) + y + floor(y / 4) - floor(y / 100) + floor(y / 400) + 1, 7) + 1
    return WeekDay[wd]
}

MsgBox, % GetWeekday(1, 1, 1990)         ; 1990-01-01 --> Monday
Hope you like it smorgasbord :P
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: GetWeekday

15 Apr 2014, 09:00

@jNiZm
I love it, sheer bliss it is to see that code.
thanks
John ... you working ?
garry
Posts: 3769
Joined: 22 Dec 2013, 12:50

Re: GetWeekday

15 Apr 2014, 09:59

interesting, has also ahk-variable dddd

Code: Select all

;-- http://www.science.co.il/language/locale-codes.asp

x=20140301             ;- saturday
FormatTime,a,%x%,dddd
msgbox,%x%=%a%
;-----------------------------------------------------

;-2nd-examples
 Time1 += -2, Days
 Time2 += -1, Days
FormatTime, Suomi   , %time1% L1035, dddd MMMM yyyy-MM-dd
FormatTime, Portugal, %time2% L2070, dddd MMMM yyyy-MM-dd
FormatTime, HereNow ,              , dddd MMMM yyyy-MM-dd
msgbox,Finland=%Suomi%`nPortugal=%Portugal%`nHere Now=%hereNow%
return
;===============================================================
User avatar
uname
Posts: 23
Joined: 25 Oct 2013, 12:50

Re: GetWeekday

15 Apr 2014, 13:11

Code: Select all

GetWeekday(month, day, year, locale := 0) {
    FormatTime wday, % ""
       . {StrLen(year)  : "20"} [2] . year
       . {StrLen(month) : "0"}  [1] . month
       . {StrLen(day)   : "0"}  [1] . day
       . " L" locale
    , dddd
    return wday
}

MsgBox % GetWeekday(4,15,14,1035)
;)
garry
Posts: 3769
Joined: 22 Dec 2013, 12:50

Re: GetWeekday

15 Apr 2014, 13:46

thank you jNizM and uname for the functions
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: GetWeekday

15 Apr 2014, 20:03

@uname
The task was to get weekdays without using any other external resource like formattime etc ( just for fun )
and make the code that has a coolness factor like Vxe's says.

Try to decipher/understand jNiZm's code, you shall surely pull your hair out for sure. :)
John ... you working ?
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: GetWeekday

24 Apr 2014, 12:45

There a small mathematical example from DOS days that I "port" to AHK:

Code: Select all

;0,7 - sunday
;1 - monday // 2 - tuesday // 3 - wednesday // 4 - thursday // 5 - friday // 6 - saturday
dayOfweek(y, m, d) ;any date gregorian calendar
;y - (all digits format) 14 will be fourteen year AD
{ y -= (a:= (14 - m) // 12)
  return mod(d + y + y//4 - y//100 + y//400 + 31*(m + 12*a - 2)//12, 7)
} ;===============================================
AHKv2.0 alpha forever.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 160 guests