GetWeekday

Post your working scripts, libraries and tools
User avatar
jNizM
Posts: 2489
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
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] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
smorgasbord
Posts: 490
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: 1871
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: 18
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: 1871
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: 490
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: 211
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 use the future now.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: c600g, Google [Bot], JoeWinograd and 61 guests