### Post

6 followers Follow # difference between 2 dates

I want to have 2 date fields and I want the calculation date to tell me the number of years between those 2. IE 11/13/2006 to 8/27/2015 equals 8.80 years.

Mike Cox 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 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 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 Thank you so much. 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 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 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 Hey Manni,

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

Rainer Hi Rainer,   Please let me know if you need anything else.

Regards, Manni Sidhu 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 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 