Post

6 followers Follow
0
Avatar

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

Please sign in to leave a comment.

12 comments

0
Avatar

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

Mitchell Ogden 0 votes
0
Avatar

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.

 

 

Hardy Desai 0 votes
0
Avatar

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

Rainer Grabowski 0 votes
0
Avatar

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

Manni Sidhu 0 votes
0
Avatar

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

Rainer Grabowski 0 votes
0
Avatar

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

Manni Sidhu 0 votes
0
Avatar

Hey Manni,

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

Rainer

Rainer Grabowski 0 votes
0
Avatar

Hi Rainer,

Please see below.

Please let me know if you need anything else.

Regards, Manni Sidhu

Manni Sidhu 0 votes
0
Avatar

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

Rainer Grabowski 0 votes
0
Avatar

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

Manni Sidhu 0 votes