why my calculated field does not always work correctly

Hi,

I have a calculated field using javascript to calculate the number of workdays between created and resolved and works correctly most of the time however will sometimes calculate incorrectly. What is wrong here?

 

<!-- @@Formula: long days(Date start, Date end){ 
//Ignore argument check
Calendar c1 = GregorianCalendar.getInstance();
c1.setTime(start);
int w1 = c1.get(Calendar.DAY_OF_WEEK);
c1.add(Calendar.DAY_OF_WEEK, -w1 + 1);
Calendar c2 = GregorianCalendar.getInstance();
c2.setTime(end);
int w2 = c2.get(Calendar.DAY_OF_WEEK);
c2.add(Calendar.DAY_OF_WEEK, -w2 + 1);
//end Saturday to start Saturday
long days = (c2.getTimeInMillis()-c1.getTimeInMillis())/(1000*60*60*24);
long daysWithoutSunday = days-(days*2/7);
if (w1 == Calendar.SUNDAY) {
w1 = Calendar.MONDAY;
}
if (w2 == Calendar.SUNDAY) {
w2 = Calendar.MONDAY;
} 
return daysWithoutSunday-w1+w2;
} 
return days(issue.get("created"),issue.get("resolutiondate"))
-->

3 answers

0 vote

This looks like JMWE plugin, not ScriptRunner.

First, the formula uses Java, not JavaScript. Second, how exactly does it "sometimes calculate incorrectly"? We need more information in order to be able to help you.

it doesnt calculate the correct number of days on all issues sometimes it will show 1 day for something created today and solved 2 minutes later when it should be 0 and some show 9 days when it should be 5 days etc, others work perfectly so im not sure what the issue is.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Posted 7 hours ago in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

26 views 0 5
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you