Module:Date Stuff: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Tag: Manual revert
 
(26 intermediate revisions by the same user not shown)
Line 25: Line 25:
end
end
end
end
return args[2]
return p._age(args)
end
end


Line 38: Line 38:


--Takes a short form date and converts it to a long form, and marks if it is only a year, or a month and year
--Takes a short form date and converts it to a long form, and marks if it is only a year, or a month and year
function convertShortDate(date, calendar)
function convertShortDate(shortDate, calendar)
   --In place of a long if elseif, I've put all the months into a single variable, with each in its numerical place.
   --In place of a long if elseif, I've put all the months into a single variable, with each in its numerical place.
   --This also allows us to add other calendars here
   --This also allows us to add other calendars here
Line 53: Line 53:
   --Takes the provided variable and seperates it out into a table
   --Takes the provided variable and seperates it out into a table
   --Also marks if it is only a year or a month and year
   --Also marks if it is only a year or a month and year
   date = Split(date, "-")
   local shortDate = Split(shortDate, "-")
    
    
   if date[2] == nil then
   if shortDate[2] == nil then
     date = {nil, nil, date[1], "year"}
     dateTable = {nil, nil, nil, tonumber(shortDate[1]), "year"}
   elseif date[3] == nil then
   elseif shortDate[3] == nil then
     date = {monthNames[tonumber(date[1])].." ("..date[1]..")", nil, date[2], "month-year"}
     dateTable = {monthNames[tonumber(shortDate[1])], tonumber(shortDate[1]), nil, tonumber(shortDate[2]), "month-year"}
   else
   else
     date = {monthNames[tonumber(date[1])].." ("..date[1]..")", date[2], date[3], "month-day-year"}
     dateTable = {monthNames[tonumber(shortDate[1])], tonumber(shortDate[1]), tonumber(shortDate[2]), tonumber(shortDate[3]), "month-day-year"}
   end
   end
 
   return dateTable
   return date
end
end


--Writes that date based on what is given.
--Writes that date based on what is given.
function writeDate(date)
function writeDate(dateTable)
   if date[4] == "year" then
   if dateTable[5] == "year" then
     return date[3]
     return dateTable[4]
   elseif date[4] == "month-year" then
   elseif dateTable[5] == "month-year" then
     return date[1].." "..date[3]
     return dateTable[1].." ("..dateTable[2]..") "..dateTable[4]
   elseif date[4] == "month-day-year" then
   elseif dateTable[5] == "month-day-year" then
     return date[1].." "..date[2]..", "..date[3]
     return dateTable[1].." ("..dateTable[2]..") "..dateTable[3]..", "..dateTable[4]
   end
   end
end
end
Line 85: Line 84:
--Calculates a persons age using the specified calendar
--Calculates a persons age using the specified calendar
function p._age(args)
function p._age(args)
   firstDate = args[1]
   local birthDate = convertShortDate(args[1], args[4])
   secondDate = args[2]
   local secondDate = convertShortDate(args[2], args[4])
  status = args[3]
   local status = args[3]
   calendar = args[4]
  --checks if firstDate is not nil, and if it is returns the second date
  if firstDate ~= nil then
    firstDate = convertShortDate(firstDate, calendar)
    secondDate = convertShortDate(secondDate, calendar)
  elseif firstDate == nil then
    return writeDate(convertShortDate(secondDate))
  end
    
    
   --Checks which math formula to use, based on how much of the date there is.
   --Checks which math formula to use, based on how much of the date there is.
   if firstDate[4] == 1 or secondDate[4] == 1 or firstDate[5] == 1 or secondDate[5] == 1 or firstDate[2] <= secondDate[2] then
   if birthDate[5] == "year" or secondDate == "year" then
    age = math.floor(secondDate[3] - firstDate[3])
    age = math.floor(secondDate[4] - birthDate[4])
  elseif firstDate[2] >= secondDate[2] then
  elseif birthDate[5] == "month-year" or secondDate == "month-year" then
    age = math.floor(secondDate[3] - firstDate[3] - 1)
    if birthDate[2] <= secondDate[2] then
      age = math.floor(secondDate[4] - birthDate[4])
    elseif birthDate[2] > secondDate[2] then
      age = math.floor(secondDate[4] - birthDate[4] - 1)
    end
  elseif birthDate[5] == "month-day-year" or secondDate == "month-day-year" then
    if birthDate[2] < secondDate[2] then
      age = math.floor(secondDate[4] - birthDate[4])
    elseif birthDate[2] == secondDate[2] then
      if birthDate[3] <= secondDate[3] then
        age = math.floor(secondDate[4] - birthDate[4])
      elseif birthDate[3] > secondDate[3] then
        age = math.floor(secondDate[4] - birthDate[4] - 1)
      end
    elseif birthDate[2] > secondDate[2] then
      age = math.floor(secondDate[4] - birthDate[4] - 1)
    end
   end
   end
    
    
   --Determines if age or aged is required
   --Determines if age or aged is required
   if status == "alive" then
   if status == "alive" then
     return writeDate(firstDate).." (age "..age..")"
     local longDate = writeDate(birthDate)
    return longDate.." (age "..age..")"
   elseif status == "dead" then
   elseif status == "dead" then
     return writeDate(secondDate).." (aged "..age..")"
     return writeDate(secondDate).." (aged "..age..")"

Navigation menu