Automation - Calculate and display difference between now and the value in a custom field
Asked by Colin Macrae:

Good afternoon!   

I've been looking for and tried various proposed solutions to similar issues but to no avail.

I've set up a series of automations that log the date/time a specific label is added to an issue into a custom field. There are 3 or 4 labels I'm interested in tracking and I've got that working without issue.  

What I would like to do next, and where my issues lie, is add fields between them that will display the time taken between the 1st label being added and the 2nd being added in another custom field. The idea is that it will show...

Label 1 added \[date\]\[time\]

Time between with label 1 added and label 2 added \[x\] d, \[x\] h, \[x\] m, \[x\] s

Label 2 added \[date\]\[time\]

Time between with label 2 added and label 3 added \[x\] d, \[x\] h, \[x\] m, \[x\] s

Issue transitioned \[date\]\[time\]

Time between label 3 added and issue transitioned \[x\] d, \[x\] h, \[x\] m, \[x\] s  

I can get it to add text into the 'time between' field, but I cannot get diff to work.  
I initially tried {{now.diff(issue.customfield_XXXXX)}} within the same rule that the date/time stamp is added but when that didn't work I tried to add a new rule watching for a change on the field and then doing a diff between the 2 custom fields but that didn't work either!  

I'm 99% sure I'm just doing something in the wrong order or the wrong place!!   

If anyone could help me out/give advice or suggestions, I would greatly appreciate it and save me from pulling my hair out any more than I already have haha. TIA


Answers:
author: Kalyan Sattaluri
body: Hello [@Colin Macrae](/t5/user/viewprofilepage/user-id/3577273)

Its hard to understand what the issue could be without screenshots of the rule and audit log.

I am sharing a screenshot of example below and let us know what is not working.

In my example, I am storing date in the format of YYYY-MM-DD in a custom field. This is a text field.

and I am logging various values for you to refer.

So when I log just {{customfield_11302}}, I get output of date but it is a string. I cannot do date operations on it yet. I have to convert it to date. So thats my second log. {{customfield_11302.toDate}}

As you can see in my third log, I can "format" this date by specifying the sequence (MM/DD/YYYY) in my case.

Finally, to do a diff, I am using the customfield.toDate to calculate the difference.

The diff function takes 2 dates.

Are you trying to subtract days from date? If so you have to use minus operator. See link further below:

[https://support.atlassian.com/cloud-automation/docs/jira-smart-values-date-and-time/#Date-minus-unit---:\~:text=Hours-,Date%20minus%20unit%20%7B%7B%5Bdate%5D.minus%5BUnit%5D(%5Bnumber%5D)%7D%7D,-Takes%20the%20given](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-date-and-time/#Date-minus-unit---:~:text=Hours-,Date%20minus%20unit%20%7B%7B%5Bdate%5D.minus%5BUnit%5D(%5Bnumber%5D)%7D%7D,-Takes%20the%20given)

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/311625i59962EC5EBAE9348/image-size/large?v=v2&px=999 "image.png")

Now can you please tell where are you stuck and share some screenshots of audit log like I have above of various fields and what is not working with diff?

comments: author: Colin Macrae
body: That helped a lot actually, the problem was not having the '.toDate'. My wife is an Oracle developer, so I also picked her brains, haha!

For context, I am tracking labels added to an issue to calculate the time to progress through various stages of testing.

* Automation 1: When label 1 is added, I log the date/time in custom field 1 with {{now}}.  

  ![Auto1.png](https://community.atlassian.com/t5/image/serverpage/image-id/312062i62CE5FFB61C2B0E3/image-size/large?v=v2&px=999 "Auto1.png")  

* Automation 2: When label 2 is added, I log the date/time in custom field 2 with {{now}} and remove the first label. I then calculate the difference between the 1st label being added and now (the addition of label 2) to show the time between those steps and log that in custom field 3.  

  ![Auto2.png](https://community.atlassian.com/t5/image/serverpage/image-id/312063iCF0AA43CEF3E884C/image-size/large?v=v2&px=999 "Auto2.png")
* Automation 3: When label 3 is added, I log the date/time in custom field 4 with {{now}} and remove the second label. I then calculate the difference between the 2nd label being added and now (the addition of label 3) to show the time between those steps and log that in custom field 5.  

  ![Auto3.png](https://community.atlassian.com/t5/image/serverpage/image-id/312064i6D0DE0CF20B2A062/image-size/large?v=v2&px=999 "Auto3.png")  

* Automation 4: When the issue is finally transitioned to the next stage, I log the date/time of that transition in custom field 6. I then calculate the difference between the 3rd label being added and now (the transition of the issue) to show the time between those steps and log that in custom field 6.  

  ![Auto 4.png](https://community.atlassian.com/t5/image/serverpage/image-id/312065i29C310AE560E1A54/image-size/large?v=v2&px=999 "Auto 4.png")

For anyone else who may be looking for similar in the future...

The eventual solution comparing a custom field containing a date/time and the current time (when the label was changed to the next stage) and then showing the difference between them is as follows:  

{{issue.customfield_10377.toDate.diff(now)}}

Thanks for your time and reply!


Tags: cloud

jira-service-management

jira-service-management-cloud