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 = { 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) 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) RE: calculate diferent between years, month, days - admin - 19.04.2022 Here's my solution Code: sday, smonth, syear = 1, 2, 1999 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') |