Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Calculate day difference correctly

Roman Wolf
Contributor
February 10, 2023

Hello everyone,

I am using JIRA Cloud and it's Automation feature. I am stucked at a minor issue someone might have a solution for:

Goal: Calculate day difference from today til a day in future

Formula: now.diff(version.releaseDate).days

The formula works and returns a value. But the result is not correct from a human point of view.

 

Assume this formula is called during Friday and releaseDate is the upcoming Monday the result of the formula is: 2

As a human I would expect: 3 and interpret 2 as Sunday.

I understand why the formula returns 2. But is there a way to achieve a result of 3?

I didn't found anything about rounding ...

 

Thanks, Roman

2 answers

1 accepted

1 vote
Answer accepted
Roman Wolf
Contributor
February 14, 2023

Thanks to @Tony Langlet I found the solution to my problem.

Root cause: The release date comes only with a date value and a zero timestamp (as there is no timestamp to configure).

The value of NOW comes with a date and a timestamp value (non-zero).

Diff both values result in a floating number which than gets converted into an integer . The internal rounding renders the result to be wrong.

Solution: One way to solve this is give the NOW value also a zero timestamp. Then the diff calculation will work out as expected.

Calling 

now.toStartOfDay

does the magic.

 

Full function:

now.toStartOfDay.diff(version.releaseDate).days

 

Regards, Roman

1 vote
Tony Langlet
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.
February 10, 2023

Hello Roman,

I guess it all depends on how you count. 

With the formula that you use would the difference between monday and tuesday be 0? If that's the case you could just add two to the calculation, if you'd like to count the current date and the end date. 

(now.diff(version.releaseDate).days) + 2

 


You could use math and round as well 

2023-02-10 16_28_30-Window.png

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

 


 

You could look in to the different date parameters and see if .businessdays works better.

https://support.atlassian.com/cloud-automation/docs/jira-smart-values-date-and-time/

 


An example that I use for checking difference in the last change in status is as follow,

{{now.diff(issue.customfield_10021.currentStatus.statusDate.jira.toDate).businessDays}}

 

I hope that some of the info I shared could be of use.

Suggest an answer

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

Atlassian Community Events