How to calculate total time spent on To Do issue excluding weekend?

How to calculate total time spent on To Do issue excluding weekend ?

One solution is to use the Time in Status custom field type provided by the JMCF app.

Can you expand on what you’re trying to ask? Are you asking from a developer perspective writing code around the Atlassian toolset that does this calculation. (If so - can you expand on what you’re trying to do?)

If you’re asking from a user perspective - there are several apps on the Atlassian Marketplace that does this type of thing but you might want to ask on the User Community: https://community.atlassian.com .

I don’t want to use any other plugin I am trying to get using script runner.Thanks for your valuable reply

I am having different workflow status e.g. ‘To Do’, ‘In Progress’ , ‘Done’ etc and I want scripts which will get time spent on each status of issue excluding weekend (Saturday and Sunday).I succeed to get for status ‘In Progress’ and ‘Done’ but unable to get for status ‘To Do’ as may be it is default status on issue creation.
Note : I am using Jira Script Runner to perform this operation
Thanks for your valuable reply.I am pasting my script here which is working fine for In progress status…by excluding weekends it is giving me correct time spent on status…I am trying the same for To Do status i.e.first status after issue creation …but not succeeding…can you please help me…
Eagerly Waiting for help…

My Code for In Progress status:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
import java.util.concurrent.TimeUnit
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.issue.history.ChangeItemBean

def statusCode = [‘Done’,‘In Progress’,‘In testing’,‘In Implementation’,‘To Do’];

IssueManager im = ComponentAccessor.getIssueManager()

//MutableIssue issue = im.getIssueObject(‘TES-24’)

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

String myStatus = “In Progress”

List rt = [0L]

long curTime = System.currentTimeMillis();

changeHistoryManager.getChangeItemsForField(issue, “status”).reverse().each { ChangeItemBean item ->

item.toString == myStatus   

Calendar c1 = new GregorianCalendar()

c1.setTimeInMillis(item.created.getTime())

int iHolydayCnt = 0;

while(c1.getTimeInMillis() < curTime)
{
    c1.add(Calendar.DAY_OF_YEAR, 1)
    
    if((c1.get(Calendar.DAY_OF_WEEK).equals(Calendar.SATURDAY)) || c1.get(Calendar.DAY_OF_WEEK).equals(Calendar.SUNDAY) ) 
        ++iHolydayCnt;  
    //log.warn(iHolydayCnt)
}          
//convert to seconds for Duration
long timeDiff = curTime - item.created.getTime() - iHolydayCnt*(60*60*24*1000)

// def timeDiff = timeStart - timeEnd
if (item.fromString == myStatus) {
    rt << -timeDiff
}
if (item.toString == myStatus) {
    rt << timeDiff
}

}

def total = rt.sum()/1000 as Long
def mytotal =DateUtils.getDurationString(total)
log.warn(total)
log.warn(mytotal)
return mytotal ?: 0L

My code for To Do status:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
import java.util.concurrent.TimeUnit
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.issue.history.ChangeItemBean

def statusCode = [‘Done’,‘In Progress’,‘In testing’,‘In Implementation’,‘To Do’];

IssueManager im = ComponentAccessor.getIssueManager()

//MutableIssue issue = im.getIssueObject(‘TES-24’)

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

String myStatus = “To Do”

List rt = [0L]
def createdDateDiff = System.currentTimeMillis() - issue.getCreated().getTime()
rt << createdDateDiff

long curTime = System.currentTimeMillis();

changeHistoryManager.getChangeItemsForField(issue, “status”).reverse().each { ChangeItemBean item ->

item.toString == myStatus   

Calendar c1 = new GregorianCalendar()

c1.setTimeInMillis(item.created.getTime())

int iHolydayCnt = 0;

while(c1.getTimeInMillis() < curTime)
{
    c1.add(Calendar.DAY_OF_YEAR, 1)
    
    if((c1.get(Calendar.DAY_OF_WEEK).equals(Calendar.SATURDAY)) || c1.get(Calendar.DAY_OF_WEEK).equals(Calendar.SUNDAY) ) 
        ++iHolydayCnt;  
    //log.warn(iHolydayCnt)
}          
//convert to seconds for Duration
long timeDiff = curTime - item.created.getTime() - iHolydayCnt*(60*60*24*1000)

// def timeDiff = timeStart - timeEnd
if (item.fromString == myStatus) {
    rt << -timeDiff
}
if (item.toString == myStatus) {
    rt << timeDiff
}

}

def total = rt.sum()/1000 as Long
def mytotal =DateUtils.getDurationString(total)
log.warn(total)
log.warn(mytotal)
return mytotal ?: 0L

Hello,

I recently solved a similar problem. It was necessary to add some special conditions. Please note that the list of changes is not in reverse order in my code bellow. I believe you will make it work with your request to ignore the weekends from here.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def inProgressName = "In Progress"
def timeNow = System.currentTimeMillis();

List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
def statusChanged = changeItems.size() != 0;
def currentStatusName = issue.getStatus().getName();

if(!statusChanged && currentStatusName == inProgressName){
    def timeDiff = timeNow - issue.getCreated().getTime();
    rt << timeDiff;
}

def changesCount = 0;
changeItems.each { ChangeItemBean item ->
    def timeDiff = timeNow - item.created.getTime();
    
    if ((item.fromString == inProgressName)) {
        if(changesCount == 0){
            rt << item.created.getTime() - issue.getCreated().getTime();
        }else{
            rt << -timeDiff
        }
    }
    if (item.toString == inProgressName) {
        rt << timeDiff
    }
    
    changesCount++;
}

def total = rt.sum() as Long
return (total / 1000) as long ?: 0L

Its working fine for calculating total time spends on particular issue including weekend but I am interesting in exclude weekend !
Can somebody help me?

Hi all,
Can anybody help me to exclude weekend from time spent in particular status ? I am not getting the required solution for the First status in my workflow.Any guidance appreciated in advance!

@SanjayDhandare

thank you so much for your script it is what i needed i use it and it works fine but It currently returns negative value in Some issue

Could anyone please advise me on how to fix this error ?