difference between 2 dates

Comments

13 comments

  • Mitchell Ogden

    Entering the following into the calculation field should yield the result your looking for:

    var start = moment(@Start Date/Time);
    var end = moment(@End Date/Time);
    var diffMS = end.diff(start) // difference in milliseconds;
    var diffDur = moment.duration(diffMS);
    var diffYear = diffDur.asYears();
    diffYear

    0
    Comment actions Permalink
  • Hardy Desai

    Hi Mitchell,

    Thanks for the script.

    I don't have Time in my date field.

    Is there a way to find out the remaining months. For example, if two dates compared then the result should be 12 months and 15 days

    Thanks in advance.

     

     

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Hardy,

    var 
    a = moment(@Date 1),
    b = moment(@Date 2),
    c = b.diff(a,"month"),
    d = b.diff(a.add(c,"months"),"days");

    c + " month " + d + " days"

    There's one caveat if both dates are the last day of the month and date day number of date 2 is smaller , e.g date 1 = 08/31/2018 , date 2 = 09/30/2019 - result would be 12 month 30 days. If you want the result 13 months 0 days you need an additional step (var cx and dx):

    var 
    a = moment(@Date 1),
    b = moment(@Date),
    c = b.diff(a,"month"),
    d = b.diff(a.add(c,"months"),"days"),
    cx = b.endOf("month").format("DD") == String(d) ? c + 1 : c,
    dx = b.endOf("month").format("DD") == String(d) ? 0 : d;

    cx + " month " + dx + " days"

    Rainer

    0
    Comment actions Permalink
  • Hardy Desai

    Thank you so much.

    0
    Comment actions Permalink
  • Manni Sidhu

    Hi,

    Can anyone tell me how to get this to work between 2 different date fields. For example Date of Absence and Date Returned to Work?

    Thanks

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Manni,

    var a = moment(@Date of Absence);
    var b = moment(@Date Returned to Work);
    b.diff(a,"d")

    Returns the diffence as number of days. 

    Rainer

    0
    Comment actions Permalink
  • Manni Sidhu

    Hi Rainer,

    Thank you for that, unfortunately I have tried this and I get the error 'Invalid Date', any idea why this would be?

    Thanks, Manni Sidhu

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hey Manni,

    can you please share a screenshot where I can see the date fields and the calculation field with the error notification?

    Rainer

    0
    Comment actions Permalink
  • Manni Sidhu

    Hi Rainer,

    Please see below.

    Please let me know if you need anything else.

    Regards, Manni Sidhu

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hello Manni,

    the notification is shown cause both date fields are empty. Both must be filled to get a result. If one of the date fields is empty  the calculation field will show nothing (= empty) in the item view. 
    Just save the template, enter dates in that item and you'll see that it works and that it returns a result (for a check: go back to the template, enter a blank space at the end of the calculation (that triggers the code and returns the preview) and you'll see that the preview is ok now)

    FYI: There are many "error" notifications which give a hint  that for the current item (where you opened the template) this calculation doesn't return a valid result. But those notifications don't mean that the code in general is wrong. If you can save it it likely means, that it doesn't work cerrectly for this item. A tip: If you create a code all involved fields should be filled.

    Rainer

    0
    Comment actions Permalink
  • Manni Sidhu

    Hi Rainer,

    As you have said I have added values into both date fields but this still does not work. The template won't save and shows the error below,

    Thanks Manni

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hey Manni,

    look at the (red) notification in the lower left corner.
    You need to add a new calculation field, copy the code and delete the current calc field.
    More about that notification and some calculation field basics you find here
    https://help.podio.com/hc/en-us/articles/201019358-Calculations

    Rainer

    0
    Comment actions Permalink
  • Daniel Gyll

    Very interesting this.

    I want to register the Customer Lifetime Value.
    Meaning im collecting the Start date for their service (date 1)
    But as long as they are not churning or ending their service it is ongoing. Therefor my (date 2) is always "now".
    So how can i get Podio to show how many months and years the customer has been paying. im using this for loyalty coins and celebrations. Can Podio help with that ?

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk