Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,294,621
Community Members
 
Community Events
165
Community Groups

How to set the display format in a behavior script on dd-mm-yyyy

Hi,

I want to copy the value of a Custom field Date picker on a Jira Service Management portal via a Behavior from an issue selected in an issue picker field.

The copy action works, but it is not displayed in the desired format.

How do I make sure it is displayed in the format dd-mm-yyyy?

 

Regards, Marco

 

current script:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager

def issueManager = ComponentAccessor.getIssueManager() as IssueManager
def issuePickerCustomFieldValue = getFieldById(getFieldChanged()).value
def relatedIssue = issueManager.getIssueByCurrentKey(issuePickerCustomFieldValue as String)
def EinddatumprojectCustomField = getFieldByName("Einddatum project")
def EinddatumprojectCustomFieldObject = customFieldManager.getCustomFieldObjectsByName("Einddatum project")[0]
def EinddatumprojectCustomFieldValue = (EinddatumprojectCustomFieldObject ? relatedIssue.getCustomFieldValue(EinddatumprojectCustomFieldObject) : null) as String
if (EinddatumprojectCustomFieldValue) {
EinddatumprojectCustomField.setFormValue(EinddatumprojectCustomFieldValue)
}

 

2 answers

1 accepted

1 vote
Answer accepted

When you retrieve the value from your custom field, don't coerce the type to String:

def EinddatumprojectCustomFieldValue = (EinddatumprojectCustomFieldObject ? relatedIssue.getCustomFieldValue(EinddatumprojectCustomFieldObject) : null) as String

Because when you do that, you get the default format and you can't change it.

Instead, keep it as a Timestamp  (the default data type for date in jira) so you can use the format method (with optional user timezone)

def EinddatumprojectCustomFieldValue = (EinddatumprojectCustomFieldObject ? relatedIssue.getCustomFieldValue(EinddatumprojectCustomFieldObject) : null) as java.sql.Timestamp
if (EinddatumprojectCustomFieldValue) {
EinddatumprojectCustomField.setFormValue(EinddatumprojectCustomFieldValue.format('dd-MM-yyyy'))

//to format date based on user tz instead of server tz
//def userTimeZone = ComponentAccessor.getComponent(com.atlassian.jira.timezone.TimeZoneManager).loggedInUserTimeZone
//EinddatumprojectCustomField.setFormValue(EinddatumprojectCustomFieldValue.format('dd-MM-yyyy',userTimeZone ))
}
2 votes
Nir Haimov Community Leader Aug 17, 2021

Hi @Marco Brundel 

When you set the date to your field , try to do it like this:

myField.setFormValue(newVal.format("dd-mm-yyyy"))

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

267 views 0 6
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you