This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

calculate diferent between years, month, days
#5
Here's my solution Smile
Code:
sday, smonth, syear = 1, 2, 1999
eday, emonth, eyear = 2, 3, 2022

monthdiff = emonth - smonth
yeardiff = eyear - syear

daydiff = eday - sday
if daydiff < 0 then
  leap = ((eyear % 4 == 0) and (eyear % 100 ~= 0)) or (eyear % 400 == 0)
  mdays = { 31, (leap and 29 or 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

  if emonth == 1 then
    emonth = 12
  else
    emonth = emonth - 1
  end

  daydiff = daydiff + mdays[ emonth ]
  monthdiff = monthdiff - 1
end

if monthdiff < 0 then
  monthdiff = monthdiff + 12
  yeardiff = yeardiff - 1
end

log(daydiff, monthdiff, yeardiff)
Reply


Messages In This Thread
RE: calculate diferent between years, month, days - by admin - 19.04.2022, 07:31

Forum Jump: