0

How to get passed time in months from two dates?

Hi there,

I hope you can help me out. 

I have two date modules:

1. Date: 01.01.2020

2. Date: 01.01.2021

How to create a formula which tells me how many months have passed? (12 months?)

In Excel I'm use to use =DATEDIF(line;row;"m"), however Ninox doesn't seem to know the command DATEDIF. 

Any idea how I can do that?

Thanks :)

4 replies

null
    • Fred
    • 9 mths ago
    • Reported - view

    Here is one way. Not as clean as Excel but it works:

    let sYear := year(Date1);
    let eYear := year(Date2);
    let numYear := if eYear > sYear then eYear - sYear else 1 end;
    let sMon := month(Date1);
    let eMon := if eYear > sYear then
            month(Date2) + numYear * 12
        else
            month(Date2)
        end;
    eMon - sMon
    

    Lines 1 - 4 takes two date fields (Date1 and Date2) and breaks out the year and month number from the dates.

    Lines 5 - 9 has to add a bit of math to account for months in the next year. The if statement check if  eYear is greater than sYear and if it is then it takes the number of years from line 3 and multiplies it by 12 then adds the month number in Date2

    Line 10 then does the easy math of subtracting sMon from eMon getting you the number of months between the two dates.

    • Fred
    • 9 mths ago
    • Reported - view

    Cause I can't leave well enough alone. The code can be cleaned up to:

    let sYear := year(Date1);
    let eYear := year(Date2);
    let numYear := if eYear > sYear then eYear - sYear else 0 end;
    let sMon := month(Date1);
    let eMon := month(Date2) + numYear * 12;
    eMon - sMon
    

    There is no check if the eYear is less than sYear.

    • Mirella_Griffiths
    • 9 mths ago
    • Reported - view

    wow, this is unbelievable, this formula works like a champ! I would have never come up with it. Thank you so much, Fred, for your help 😀

    • Mel_Charles
    • 9 mths ago
    • Reported - view

     Way to go man. You just solved a problem for me to that I did not know I had.

    Just got asked this week by one of my team - if I could add the facility to calculate when a customer orders a repeat job - How long was it since they ordered the last occurrence.

    Hadn't even got around to looking at it.

    Your script is perfect - Thanks buddy :-)

Content aside

  • Status Answered
  • 9 mths agoLast active
  • 4Replies
  • 56Views
  • 3 Following