Announcement

Collapse
No announcement yet.

Creating Get Age from DOB

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating Get Age from DOB

    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
    Plugins or Sources MokoX
    BunnyHop Here

  • #2

    the function os.date is limited you cant go back more than 1970

    Comment


    • #3
      Ya I ended up coming up with that thought also, I was going to try do the maths for it but I gave up as it was only for my partner to lean a little in ams, she gave up tho.
      Plugins or Sources MokoX
      BunnyHop Here

      Comment


      • #4
        Not sure if this is of any help : https://stackoverflow.com/questions/...e-dates-in-lua

        Comment

        Working...
        X