--date=and a year as parameter |year= (uses current year if omitted)and returns the date in YYYY-MM-DD format, e.g. 2018-06-08Other formats are possible.
require ('strict')
--is_set(var)Whether variable is set or not. A variable is set when it is not nil and not empty.local function is_set(var) return var and var ~= end
--current_yearreturns the current yearlocal function current_year return os.date('%Y', os.time)end
--local function decode_date_event(date_event_string) local ordinals = local days = local months =
date_event_string = date_event_string:lower local ord, day, month = date_event_string:match ('([%a%d]+)%s+(%a+)%s+%a+%s+(%a+)')
if is_set(ord) and is_set(day) and is_set(month) then return ordinals[ord], days[day], months[month] else -- if one or more of these not set, then pattern didn't match return nil endend
--get_days_in_month(year, month)Returns the number of days in the month where month is a number 1–12and year is four-digit Gregorian calendar.Accounts for leap year. Throws an error if month and year are not numbers.local function get_days_in_month(year, month) local days_in_month = year = tonumber (year) month = tonumber (month) if month
0 and (year%100 ~= 0 or year%400
--get_date_month_day(date_event_string, year)Return the date for the day that is the ordinal (nth) day-name in month of the given yeare.g. "second Friday in June", 2018 -> 2018-06-08local function get_date_month_day(date_event_string, year) local ord, weekday_num, month = decode_date_event(date_event_string) if not (is_set (ord) and is_set (weekday_num) and is_set (month)) then return nil end if ord
-- set up public functions for debugginglocal p = p.currentYear = current_yearp.getDateMonth = function(frame) local m, d = get_date_month_day(frame.args.date, frame.args.year) return m .. "-" .. dend--date=and a year as parameter |year=and returns the date in YYYY-MM-DD format, e.g. 2018-06-08Other formats are easy to implement.p.getYearMonthDay = function(frame) local args = frame.args or frame:getParent.args local date_event = args.date or mw.text.trim(args[1]) local year = args.year or args[2] or current_year local month, day = get_date_month_day(date_event, year) month = (month<10 and "0" or "") .. month day = (day<10 and "0" or "") .. day return year .. "-" .. month .. "-" .. dayendreturn p