Calculate months between two dates
Asked by NUH:

Hello,   

I created an Automation Rule to calculate the total months between start and end date fields and then store the result in a Customfield.

How can I round the number of months ? because I got a wrong result, 1 month is missing.   

This is my smart value:

{{issue.end date.diff(issue.start date).months.abs}}  

![AutomationRules.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/335922i929B0D46B173ED3C/image-size/large?v=v2&px=999 "AutomationRules.PNG")

Thank you,

Nouha


Answers:
author: Bill Sheboy
body: Hi [@NUH](/t5/user/viewprofilepage/user-id/5474227)

For a question like this for diff() not working as expected, **please provide the example date values, the result returned, and what you expected to happen**. That will provide context for the community to offer better suggestions.

Until we see those...

The date / time function diff() *only returns integer values in the units of measure selected* (e.g., months). Depending on the units, it may use [floor, ceiling, or rounding](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-math-expressions/#Functions) adjustments.

To take control of the difference and explicitly round to the desired precision, you could do this:

* perform the diff in a smaller unit of measure,
* divide the value with a number for the conversion between the two values,
* wrap that with the [round() function](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-math-expressions/#Functions), and
* wrap that inside of a math expression.

For example, to do this for rounded days from seconds to 2 decimal places, where 86400 is the number of seconds in a 24h day:

{{#=}} round( {{issue.dateA.diff(issue.dateB).seconds}} / 86400, 2) {{/}}

The challenge for your scenario is your units are months, and those have a variable number of days per month. And so seeing the specific date values you are using may indicate what is happening.

Kind regards,  
Bill

comments: author: NUH
body: Hello Bill,   
Thank you for your response!  
This is my input :   

![Start_end_date.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/336665i4EDCED9424B91A00/image-size/large?v=v2&px=999 "Start_end_date.PNG")

The result I got:   
![Res.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/336664i9323E724F73EE3ED/image-size/large?v=v2&px=999 "Res.PNG")  
The result expected is 12 months instead of 11


author: Bill Sheboy
body: When I try that **same smart value expression** with two date fields it works and returns 12 months.

Let's try this to help diagnose what is happening:

* Using the Log Action, write the following to the audit log before you create the variable. Please note I am using the exact same smart values you noted.

```
  start date: {{issue.start date}}
  end date: {{issue.end date}}
  end date plus 1: {{issue.end date.plusDays(1)}}
```

* Run your test again
* Post images of your updated, complete automation rule and of the audit log details showing the rule execution

This will confirm if your smart values are correct and if there are potentially multiple fields with the exact same names (and so which are confusing the rule processing).


author: NUH
body: Hello Bill,   

With the previous dates provided I am getting the correct result using plus 1 function, but if start/end dates changes how can I handle to round months ?

For the following example : I would like to count January to have 13 months as total   

![Log.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/336954i224AD2D718466F6D/image-size/large?v=v2&px=999 "Log.PNG")  
Thank you!


author: Bill Sheboy
body: Thanks for that additional example to help clarify the need: you want to count the months difference including the end points (rather than just the difference in month values for the dates).

One solution approach is to change the end date to the [endOfMonth](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-date-and-time/#End-of-month---) first and then increment by one day. That will handle differences in the number of days per month.

```
{{issue.Start date.diff(issue.end date.endOfMonth.plusDays(1)).months}}
```


author: NUH
body: Thank you Bill.   
It is working perfectly !


author: Trudy Claspill
body: Hello [@NUH](/t5/user/viewprofilepage/user-id/5474227)

Welcome to the Atlassian community.

Use the **round** function.

<https://support.atlassian.com/cloud-automation/docs/jira-smart-values-math-expressions/#Round>


Tags: cloud

jira-service-management

jira-service-management-cloud