Page 1 of 1

GetWeekday

Posted: 15 Apr 2014, 08:58
by jNizM
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

Re: GetWeekday

Posted: 15 Apr 2014, 09:00
by smorgasbord
@jNiZm
I love it, sheer bliss it is to see that code.
thanks

Re: GetWeekday

Posted: 15 Apr 2014, 09:59
by garry
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
;===============================================================

Re: GetWeekday

Posted: 15 Apr 2014, 13:11
by uname

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)
;)

Re: GetWeekday

Posted: 15 Apr 2014, 13:46
by garry
thank you jNizM and uname for the functions

Re: GetWeekday

Posted: 15 Apr 2014, 20:03
by smorgasbord
@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. :)

Re: GetWeekday

Posted: 24 Apr 2014, 12:45
by _3D_
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)
} ;===============================================