Quantcast
Channel: Indigo Rose Software Forums - Forums
Viewing all articles
Browse latest Browse all 2105

Creating Get Age from DOB

$
0
0
I have been adapting a function from Lua to ams to show the users there age via year and month what works pretty well but if you set it to lets say 1960 or 1900
it keeps returning 47, I am confused, could anyone shade some light on this?


This is bad demo code so please bear with it lol, it works for lets say 1987 3 3 but lets say you put anything blow 1970, you get 47 no matter what so if you put 1969 1960 1900 all return 47
anyone know why this is ?


Code:
tick = {};
function isLeapYr(Year)
   if math.fmod(Year/400,1) == 0 then
      return true
   end

   if math.fmod(Year/4,1) == 0 and math.fmod(Year/100,1) ~= 0 then
      return true
   end
   return false
end

function tick.diff(nYear, nMonth, nDay)
   local dob = {year = nYear, month = nMonth, day = nDay}

   local T = os.time();
   local ageSecs = os.difftime(T, os.time{
         year = dob.year,
         month = dob.month,
         day = dob.day})

   local currY = tonumber(os.date('%Y'));
   local yrs = 0
   local YRSEC = 365*24*3600
   local LPYRSEC = 366*24*3600
   -- without this is was returning 15 for a 3 month change, 1988 3 22 returns 29 3, but without it it returned 29 15, not sure why.
   Monthi = tonumber(String.Replace(System.GetDate(DATE_FMT_MONTH), "0", "", false));
   local ageMths = 0;
   if Monthi > dob.month then
      ageMths = Monthi - dob.month
   elseif Monthi == dob.month then
      ageMths = 0
   else
      ageMths = 12 + (Monthi - dob.month)
   end

   local secs = ageSecs
   local testi = 0;
   for i = nYear, currY do
       testi = testi +1;

      if isLeapYr(i) then
         secs = secs - LPYRSEC
         yrs = yrs + 1
         if secs < YRSEC then
            return yrs, ageMths, yrs + secs/YRSEC, secs, testi
         end
      else
         secs = secs - YRSEC
         yrs = yrs + 1
         if isLeapYr(1 + 1) then
            if secs < LPYRSEC then
               return yrs, ageMths, secs/LPYRSEC, secs, testi
            end
            else
            if secs < YRSEC then
               return yrs, ageMths, yrs + secs/YRSEC, secs, testi
            end
         end
      end
   end
   return "Woops";
end

Viewing all articles
Browse latest Browse all 2105

Trending Articles