Forums

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

How to set the curr time on a custom field to curr time of the user's timezone using Behaviour

Maria DePasquale
Contributor
January 30, 2020

I need to have Incident End default to now as I close a ticket.  Since I can't use Default value, I'm using a Jira Behaviour instead.

This script works EXCEPT the date/time is off by 12 hours when the timezone is not the same as the default time zone.  If it's 5 PM in the US Central tz and 11 PM in the UK, the UK user closing a ticket has the Incident End set to 11 AM instead of 11 PM.  It works correctly for a user in the US Central tz.  What am I doing wrong?

@BaseScript FieldBehaviours fieldBehaviours

def dateField = getFieldByName("Incident End")

def timeZoneManager = ComponentAccessor.getComponent(TimeZoneManager)
def loggedUserZoneId = timeZoneManager.loggedInUserTimeZone.toZoneId()

DateFormat formatter = new SimpleDateFormat("dd/MMM/yy h:mm a")

def newLocalDateTime = LocalDateTime.now()
def newDate = Date.from(newLocalDateTime.atZone(loggedUserZoneId).toInstant())
dateField.setFormValue(formatter.format(newDate))

 

 

 

 

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Champion
January 31, 2020

Hi @Maria DePasquale ,

Not sure if this will solve your issue, but this is the script I am using and it is working fine (it's very similar) : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.timezone.TimeZoneManager

@BaseScript FieldBehaviours fieldBehaviours

def userTimeZone = ComponentAccessor.getComponent(TimeZoneManager).getLoggedInUserTimeZone()
SimpleDateFormat isoFormat = new SimpleDateFormat('dd/MM/yy HH:mm');
isoFormat.setTimeZone(userTimeZone);

def dateField = getFieldByName("Incident End")
dateField.setFormValue(isoFormat.format(new Date()))

Antoine

Maria DePasquale
Contributor
January 31, 2020

Thank you @Antoine Berry  This works!!!

Like Antoine Berry likes this

Suggest an answer

Log in or Sign up to answer