Calculate # of Months between 2 dates

Comments

19 comments

  • Rainer Grabowski

    Hi Scott, this should work:

    var start = @start date;
    var end = @end date;
    
    var diffMS = end.diff(start)  // difference in milliseconds;
    var diffDur = moment.duration(diffMS);
    var diffMonth = Math.round(diffDur.asMonths()*10)/10;
    diffMonth
    

    Results for your examples: 12.00 and 0.50

    Rainer
    rg@delos-consulting.com

    0
    Comment actions Permalink
  • Mitchell Ogden

    @Rainer

    Your Solution failed to work for me... Podio comes back with "Script error: TypeError: undefined is not a function"
    var pick = @Actual Pick Date/Time;
    var drop = @Actual Drop Date/Time;
    var diffMS = drop.diff(pick);
    var diffDur = moment.duration(diffMS);
    diffDur

    0
    Comment actions Permalink
  • Rainer Grabowski

    Sorry, a mistake on my part. Start and end must also be "moments".

    Here's the right solution:

    var start = moment(@start date);
    var end = moment(@end date);
    
    var diffMS = end.diff(start)  // difference in milliseconds;
    var diffDur = moment.duration(diffMS);
    var diffMonth = Math.round(diffDur.asMonths()*10)/10;
    diffMonth
    

    Rainer

    0
    Comment actions Permalink
  • Mitchell Ogden

    This was my working solution for my use case:

    var start = moment(@Actual Pick Date/Time);
    var end = moment(@Actual Drop Date/Time);

    var diffMS = end.diff(start) // difference in milliseconds;
    var diffDur = moment.duration(diffMS);
    var diffDay = Math.round(diffDur.asDays()*10)/10;
    diffDay + " day(s)"

    0
    Comment actions Permalink
  • Rainer Grabowski

    Mitchell,

    simply:
    var start = moment(@start date);
    var end = moment(@end date);

     end.diff(start,"days")
    

    or more exactly: use my other calculation, but instead of "asMonths" enter "asDays".

    Rainer

    0
    Comment actions Permalink
  • Mitchell Ogden

    Thanks for the simplification @Rainer !!!

    Mitch

    0
    Comment actions Permalink
  • 0
    Comment actions Permalink
  • Scott Anderson

    Hello Rainer, Thanks for your efforts. I tried your 'right solution' version that you posted. It sort of works...

    If the Start Date is 8-1-2015, and the end date is 12-31-2015 - the calculated answer comes up as "5.10". I would have expected "5".

    If I the Start Date is 02-01-2015 and the end date is 12-31-2015 - the answer is "11.10".. so something is off.

    Any thoughts on how to fix? Thanks Rainer.

    0
    Comment actions Permalink
  • Rainer Grabowski

    Scott , it's a pain to get the right number for months cause a month's length is defined "30 Days".

    This would show you 5 for 8/12015 - 12/15/2015

    var start = @start date;
    var end = @end date;

    var diffMS = end.diff(start) // difference in milliseconds;
    var diffDur = moment.duration(diffMS);
    var diffMonth = Math.round(diffDur.asMonths());
    diffMonth

    But it would you show "5" also for 8/12015 - 12/15/2015 and 8/12015 - 12/25/2015
    and "4" for 8/12015 - 12/5/2015

    What would you expect for:
    a) 8/12015 - 12/15/2015
    b) 8/12015 - 12/5/2015
    c) 8/12015 - 12/25/2015
    ?

    0
    Comment actions Permalink
  • Scott Anderson

    Thanks Rainer. I knew dates were tricky but thought someone would have worked something out to address the problem.

    I actually tried your last calc but Podio gave me "Script Error; Type Error; undefined is not a function". ??

    0
    Comment actions Permalink
  • Rainer Grabowski

    Always the same mistake, shouldn't only coypy & paste. Start and end must also be "moments".:

    var start = moment(@start date);
    var end = moment(@end date);

    0
    Comment actions Permalink
  • Scott Anderson

    I am the king of Copy/Paste. :-)

    Thanks for the clarification... I will try it out.

    0
    Comment actions Permalink
  • James Bowie

    What about if you have a contract that starts on a date and expires 180 days from the start date, and you want the calculation field to tell you what the date of expiration is?

    Expressed like this:

    var1=(@start_date)
    var2=(@duration_days)

    result = Date of expiration

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi James,

    var date  = @startdate ;
    var durD = @durationdays;
    
    moment(date).add(durD,'days').toDate()
    

    One tip: Numbers are not allowed as first characters in var names. So can't do var = 1 or var var = 1a, but var = a1.

    Rainer

    0
    Comment actions Permalink
  • James Bowie

    Rainer, thank you very much!

    Strangely, I used the formula and input 180 days with June 8, 2015 as the start date and it returned: April 5, 2027

    screenshot: https://www.dropbox.com/s/v5l8yo94fwcggs4/closed-loans.jpg?dl=0

    0
    Comment actions Permalink
  • Rainer Grabowski

    Values from a duration field are always given back as hours. So it calculates var durD = 180*24.
    Change var durD to : @duration/24

    0
    Comment actions Permalink
  • Scott Anderson

    @Rainer - I need to calculate a slightly different version of this and was hoping you could help as I've pulled out all my hair!  I need to calculate the # of days between a field (@.Last Update) and Today's actual date. 

    This is what I have, based on this post, but I really need to replace the field @.CreateDate with today's actual date.  Any thoughts on how to accomplish this??  Thank you.

     

    var start = moment(@.Last Update);
    var end = moment(@.Create Date);

    var diffMS = end.diff(start) // difference in milliseconds;
    var diffDur = moment.duration(diffMS);
    var diffDay = Math.round(diffDur.asDays()*10)/10;
    diffDay + " day(s)"

    0
    Comment actions Permalink
  • Scott Anderson

    Thanks Rainer, I will check it out.  Much appreciated!  Have a great day!

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk