Posts: 167
Threads: 20
Joined: Apr 2017
Reputation:
2
16.04.2022, 13:28
(This post was last modified: 16.04.2022, 13:30 by Dré.)
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
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
17.04.2022, 13:52
(This post was last modified: 17.04.2022, 13:53 by Erwin van der Zwart.)
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)
Posts: 167
Threads: 20
Joined: Apr 2017
Reputation:
2
18.04.2022, 13:33
(This post was last modified: 18.04.2022, 13:36 by Dré.)
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
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
19.04.2022, 07:30
(This post was last modified: 19.04.2022, 07:31 by Erwin van der Zwart.)
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
Posts: 7773
Threads: 42
Joined: Jun 2015
Reputation:
447
Here's my solution
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)
Posts: 167
Threads: 20
Joined: Apr 2017
Reputation:
2
27.04.2022, 13:18
(This post was last modified: 27.04.2022, 13:19 by Dré.)
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 ')
|