Forums

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

Date Type Script Field returning '$datePickerFormatter.format($value)'

Jake Jollimore August 28, 2018

I'm using ScriptRunner to create custom Script Fields, and my most recent one is to display the date an issue was last assigned in the format 'Aug 20, 2018'.  That field is called 'Date Assigned'

Currently, the field is set as a Text Field type, but I want to be able to use filters/functions on that field like 'Date Assigned <= 10d' in my reports.

However, when I change the field type to 'Date Time Picker' or 'Absolute Date Time', I always get the following output:

'$datePickerFormatter.format($value)' or 'Invalid Date'

What am I doing wrong? P.S. If keeping the format of 'Aug 20, 2018' is going to prohibit me from changing it to a Date type field, I can use a different format.

Here's the script:

import com.atlassian.jira.issue.*
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
List<ChangeItemBean> changeItems = changeHistoryManager.getChangeItemsForField(issue, "assignee") //Get assignee change history

Date assignedDate //This will contain the most recent assigned date, if one exists

if(changeItems != null && !changeItems.isEmpty()){
changeItems.each{ci ->
if(new Date(ci.getCreated().getTime()) > assignedDate){
assignedDate = new Date(ci.getCreated().getTime())
}
}
}

if(assignedDate != null){
assignedDate.format("MMM d, YYYY")
} else {
"This issue has yet to be assigned."
}

 

1 answer

1 accepted

1 vote
Answer accepted
Tarun Sapra
Community Champion
August 28, 2018

Hello @Jake Jollimore

Please see this answer

https://community.atlassian.com/t5/Adaptavist-questions/Date-format-for-script-field/qaq-p/698130

Here you can set the custom format for displaying your date and also use the searcher "Date time range picker" so that you can query it in JQL based on your requirements.

Tarun Sapra
Community Champion
August 28, 2018

So for the scripted field the template is custom but the searcher is "Date time range picker", searcher can be configured in the custom field configuration section. 

Jake Jollimore August 28, 2018

This is great. Thank you! However, none of the options available in the dateTimeStyle link allow me to get the 'Aug 20, 2018' format I was hoping for. Is it possible to get a custom format?

Tarun Sapra
Community Champion
August 28, 2018

You can set the custom format using the method withStyle(String) in DateTimeFormatter

Jake Jollimore August 28, 2018

No matter what string I pass, eg:

$datePickerFormatter.withStyle('').format($value)
$datePickerFormatter.withStyle('MMM d, YYYY').format($value)
$datePickerFormatter.withStyle('mm:ss').format($value)
$datePickerFormatter.withStyle('dmy').format($value)

I keep getting the same output format:

 28/Aug/18 12:19 PM

Any thoughts?

Tarun Sapra
Community Champion
August 29, 2018

I also tried different formats but getting the date like 29/Aug/18 instead of excepted format. 

Jake Jollimore August 29, 2018

Posted a new question HERE to try to figure out the formatting issue.

Suggest an answer

Log in or Sign up to answer