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
#1
Hi,

I try to make a script to calculate years, month days between 2 dates.
I will check like 2 September 2006 and today (16 April 2022)

output must be: 15 years, 7 months, 14 days

i try to make this, but it is really hard for,
i can calculate years 2022 - 2006 - 1 = 15

but if i m with months like this, April = 4, September is 9
4 - 9 = crashing for me
Because it is negative, and it has to take an extra year of the previous calculation.

And the same with days.

Did someone already think how to do this, or is there already an example for doing this?

Before I was thinking to do it with seconds, but that's even harder I think, because there are months like 28 / 29(once a 4 years) / 30 or 31
Reply
#2
Not fully tested but something like this could work (however always tricky with leap years):
Code:
startdate = {
  day = 2,
  month = 9,
  year = 2006
}

enddate = {
  day = 16,
  month = 4,
  year = 2022
}

unixstart = os.time(startdate)
unixend = os.time(enddate)

if unixend < unixstart then
  log('Start date is higer then the end date, exiting script')
  return
end

seconds = os.difftime(unixend, unixstart)
timediff = os.date('!*t', seconds)

years = timediff.year - 1970
months = timediff.month - 1
days = timediff.day - 1

log(years,months,days)
Reply
#3
Hi Erwin,

Mostly looks great, sometimes is has a miscalculation of 1 day, but i couldn't figure it out why.

Another thing, what i will try to fix, is when the first date is before 1970.
i changed
Code:
unixend = os.time(enddate)
with
Code:
unixend = os.time()
Reply
#4
Hi, I the day difference is probably due to a leap year in your start or end date, you could try to add something like this in your code:
Code:
function isLeapYear(year)
   if ((((year % 4 ==0) and (year % 100~=0)) or (year % 400==0)))then
      return true
   else
      return false
  end
end

if isLeapYear(startdate.year) then
  -- add or remove a day
  log('startdate is a leap year')
end

if isLeapYear(enddate.year) then
  -- add or remove a day
  log('enddate is a leap year')
end
Reply
#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
#6
Thanks, I choose to use the one of admin and changed it a little, so he calculates the different between today and the set day.
But both thanks for helping me.

Code:
now = os.date('*t')


eday = now.day
emonth = now.month
eyear = now.year

-- bevrijdingsdag
sday, smonth, syear = 5, 5, 1945

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

grp.checkupdate('62/7/51', yeardiff ..' jaar ' .. monthdiff ..' maanden ' .. daydiff ..' dagen ')
Reply


Forum Jump: