Set field value : Is there way to get only the time value from date time picker custom field?

Deleted user November 18, 2017

Hello,

 

I am using set field value (from groovy expression) post function. 

My requirement is to update a select list field (with options shift 1 m, shift 2, shift 3 etc) based on the "hour" value which should be extracted from custom field date time picker type. 

I use the groovy expression : 

issue.getAsString("Begin Date")

which is giving complete timestamp including date as shown in screenshot below: 

timevalue.pngCan i extract only the time value? 

I appended getTimeString() to the above code but it doesnt seem to work.

 

Any help?

 

Thanks!

Swathi

1 answer

1 accepted

0 votes
Answer accepted
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 18, 2017

Hi,

you can use all the methods of the Date class on the result of issue.get("Begin Date"): https://docs.oracle.com/javase/7/docs/api/java/util/Date.html

For example, to get the hours:

issue.get("Begin Date").getHours()

 Note that Java doesn't have a class to represent just a time value (without a date), so you'll have to manipulate the hours, minutes and seconds as numbers.

Deleted user November 18, 2017

Thanks @David Fischer !

It worked.

 

Regards

Swathi

shiva November 19, 2020

@David Fischer 

Using set field value postfunction

i have tried to copy, ( time-to-resolve) scripted field value in seconds,

to a text field( time-to-resolve(())

which gives output in seconds, 

how do i get the value in (prettyduration format) ? ex( 2w 3d 1m)

 image.png

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2020

Hi Shiva,

you can use this:

${com.atlassian.core.util.DateUtils.getDurationString(issue.get("Time-to-resolve"))}
shiva November 20, 2020

@David Fischer 

 

Thank you,

That work like a charm!!

 

can i get in jira format ( where it takes jira date format)

where 5 days a week and 8 hours a day ( like prettyduration)

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 21, 2020

Yes, use this instead:

getComponent(com.atlassian.jira.util.JiraDurationUtils).getShortFormattedDuration(issue.get("Time-to-resolve"))
shiva November 21, 2020

Thanks David,

i have added this to post function, But i get the text copied to field as below,

image.png

 

Text is directly copied to the field "time-to-receive(())" text field

     Did i miss any fixes ?

 

image.png

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 21, 2020

Sorry, I provided the Groovy code. In a Groovy template, just wrap it with ${ and } just like in the previous example:

${getComponent(com.atlassian.jira.util.JiraDurationUtils).getShortFormattedDuration(issue.get("Time-to-resolve"))}

just  

Like shiva likes this
shiva November 21, 2020

it Works!!

 

Thanks for the response.

 

last step. ( the scipted field shows the result as below)

image.png

but the copied text shows,

image.png

the script i use is as below, should i divide by 3600 , to get the data as ( 8hours perday,5days per week)

 

 

==============================================

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.Issue;
import com.atlassian.core.util.DateUtils

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()


def inprogressname = "Resolve"

List<Long> rt = [0L]

def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
changeItems.reverse().each { ChangeItemBean item ->

//def timeDiff = item.created.getTime() - issue.getCreated().getTime()

def timeDiff = System.currentTimeMillis() - issue.getCreated().getTime()

if (item.fromString == inprogressname) {
rt << -timeDiff
}
if (item.toString == inprogressname){
rt << timeDiff
}

}


//return (Math.round(((rt.sum() as Long) / 3600) as Long))

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

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 21, 2020

No. You are calculating the field value as a time difference between two date/time values. This is, by definition, an absolute duration in calendar time, meaning it includes nights and weekends. Therefore, you can't convert it/display it in working time, because it would require calculating the time, between the two date/times, spent during working hours only, which is far from trivial.

shiva November 21, 2020

Got it , thanks .!! David
Thanks for the help.


Can you direct me where I can Browse >> (calculating time in all statuses of workflow)sum of all the statuses.

or any script you are aware of which gives time between statuses with nights & weekends  excluded

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 21, 2020

I'm not sure I understood the first question. As for the second, it's not as easy as it looks, as you need to take time zones into account, and potentially holidays, vacations, etc.

Maybe someone can share their solution? I don't have a good one for that.

shiva November 21, 2020

the script i have is for adding time in status for single status,

 

How do i add time in two statuses, when i add 1st status open it shows blank,

 

if i add other statuses , other than the first it gives an output.

Luz Silverio December 5, 2021

Hi @David Fischer ,

I tried using same as above syntax (one with the post-func) but it doesn't work on my config set-up...

scripted customfield to hours is:
TOTAL TIME TO: Ticket Age (hrs) = 

while customfield destination to copy in prettyformat is:
PretDur Ticket Age

Applied your reco syntax: 

${com.atlassian.core.util.DateUtils.getDurationString(issue.get("TOTAL TIME TO: Ticket Age (hrs) ="))}

result: it did not display at all the 'PretDur Ticket Age' field

i tried adding a set of { } in curr syntax as experiment;
it displayed the 'PretDur Ticket Age field but value is single closed bracket }

Screen Shot 2021-12-06 at 2.32.17 AM.png

can you pls. help to isolate what I'm missing? thanks.

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 5, 2021

Hi @Luz Silverio ,

The question and solution we're for Jira Server/DC. JMWE for Jira Cloud uses a different syntax. You should create a separate Community question, or check out the appropriate product documentation for the cloud. 

Luz Silverio December 6, 2021

oh got that - thanks @David Fischer 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events