Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Calendar date/recurring
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
--[[ Calculates the Gregorian date of a recurring holiday that varies year-to-year, but follows the rule "Nth [day of week] of [month]" "month" = month number (1 to 12) "weeknumber" = number of week (1 to 4, or -1 to mean "last") "dayofweek" = number that represents the day of the week, where 1 = Sunday and 7 = Saturday "year" = Gregorian calendar year ]] require('strict') local p = {} function p.calculate(frame) local args = frame.args local ONE_DAY = 86400 -- number of seconds in one day local year = tonumber(args.year) local month = tonumber(args.month) local weeknumber = tonumber(args.weeknumber) local dayofweek = tonumber(args.dayofweek) local date = os.time{year=year, month=month, day=1} local dateparts = os.date("*t", date) if weeknumber > 0 then -- find the first [dayofweek] of this month while dateparts["wday"] ~= dayofweek do date = date + ONE_DAY dateparts = os.date("*t", date) end -- add the correct number of weeks if weeknumber > 1 then date = date + ((weeknumber - 1) * (7 * ONE_DAY)) end else -- find the first day of the next month while dateparts["month"] == month do date = date + ONE_DAY dateparts = os.date("*t", date) end -- go back one day to get the last day of the month we want date = date - ONE_DAY dateparts = os.date("*t", date) -- go backwards until we find the right day of week while dateparts["wday"] ~= dayofweek do date = date - ONE_DAY dateparts = os.date("*t", date) end end local result = os.date("%Y-%m-%d", date) return result end return p
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Error
(
edit
)
Template:Module other
(
edit
)
Template:Module rating
(
edit
)
Template:Ombox
(
edit
)
Template:Sandbox other
(
edit
)
Module:Arguments
(
edit
)
Module:Calendar date/recurring/doc
(
edit
)
Module:Error
(
edit
)
Module:Message box
(
edit
)
Module:Message box/configuration
(
edit
)
Module:Message box/ombox.css
(
edit
)
Module:String
(
edit
)
Module:Yesno
(
edit
)