Logic Machine Forum
calculate diferent between years, month, days - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: calculate diferent between years, month, days (/showthread.php?tid=3998)



calculate diferent between years, month, days - Dré - 16.04.2022

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


RE: calculate diferent between years, month, days - Erwin van der Zwart - 17.04.2022

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)



RE: calculate diferent between years, month, days - Dré - 18.04.2022

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()



RE: calculate diferent between years, month, days - Erwin van der Zwart - 19.04.2022

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



RE: calculate diferent between years, month, days - admin - 19.04.2022

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)



RE: calculate diferent between years, month, days - Dré - 27.04.2022

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 ')