Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate months between two dates

NUH July 16, 2024

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

Thank you,

Nouha

2 answers

1 accepted

0 votes
Answer accepted
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 16, 2024

Hi @NUH 

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 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, 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

NUH July 19, 2024

Hello Bill, 
Thank you for your response!
This is my input : 

 Start_end_date.PNG

The result I got: 
Res.PNG
The result expected is 12 months instead of 11 

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2024

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).

 

NUH July 22, 2024

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
Thank you!

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 22, 2024

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 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}}

 

NUH August 7, 2024

Thank you Bill. 
It is working perfectly ! 

Like Bill Sheboy likes this
0 votes
Trudy Claspill
Community Champion
July 16, 2024

Hello @NUH 

Welcome to the Atlassian community.

Use the round function.

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events