Date format for script field

d-sokolovskiy January 4, 2018

Hi

  I have created a Date Time Picker script field, Is there anyone can help me remove the Times tamp on my return. Currently field " MM/dd/yyyy hh:mm a (30.04.2018 00:00) " and i will like to get " MM/dd/yyyy (30.04.2018)" this format instead.

Thanks

3 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
8 votes
Answer accepted
Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2018

Hey,

You can customise the date output using a ScriptRunner script field whilst maintaining the current level of search functionality with use of a "Custom" type field.

1. Create a new script field with the following fields set as specified:

Template: "Custom"

Custom Template: 

$datePickerFormatter.withStyle($dateTimeStyle.DATE).format($value)

Here are some useful docs regarding custom templates

Inline Script: (This is for testing purposes, you can put whatever script you want here so long as it returns a date)

def date = new Date()
date.previous()

2. Once you click "Add" to add the new field, you need to change the "Searcher" (see here). If you select it as a "Date Time Range picker" then the value will be searchable from within JQL as a date.

NOTE: Script fields are not updated in the Lucene index until either a re-index is run or the individual issue is updated. Script fields are best used for calculating values based on other values in the issue.

I hope this helps!

Steve

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2018

Pretty useful,  I hope the documentation is updated to include examples like the one which you just shared.

$datePickerFormatter.withStyle($dateTimeStyle.DATE).format($value)

 

David Sumlin January 23, 2018

Thank you Stephen.  We use this exact technique!

The only side-effect to this technique that I can see is that the "dates" now will display in the main issue panel.  For some users, this is a pro, others a con.

The following statement needs to be in bold AND red AND larger font AND blink!  It's important! 

**

NOTE: Script fields are not updated in the Lucene index until either a re-index is run or the individual issue is updated. Script fields are best used for calculating values based on other values in the issue.

**

If your scripted field is getting values from a db or a JQL search or something besides the issue itself (or I think a linked issue), then you're values will be out of synch with your Lucene index. (lesson learned)

Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 24, 2018

Hey @David Sumlin and @Tarun Sapra

I'm glad my answer was enlightening! If you think that others would benefit from my answer, feel free to mark it as accepted so others can more easily benefit from it.

I believe the indexing detail is already specified in the documentation:

It’s also possible to search on these values if you set up one of the four shipped searchers. If you do, the value is calculated and stored in the index at the time that the issue is indexed, eg when it is modified or transitioned, or when you do a full reindex.

 

You can only reliably use a calculated field to display a value that is based on the issue’s fields, or subtasks' fields, or linked issues. The reason why is because the value in the Lucene index is only updated when the issue is updated.
Jan Zítka July 2, 2019

Hi @Stephen Cheesley _Adaptavist_ 

 

can you tell me, how i can show(custom template) in Dates column?

 

If i used your custom template, i see value in Details column. 

$datePickerFormatter.withStyle($dateTimeStyle.DATE).format($value)

 

I set the"Date Time Range picker" in property of custom field.

 

Thanks.

Mike L October 2, 2019

This doesn't work for me using the example exactly as is. The result still includes the timestamp.

Script:

def date = new Date()
date.previous()

Custom formatter:

$datePickerFormatter.withStyle($dateTimeStyle.DATE).format($value)

Using the preview, I get "10/01/2019 11:25 AM"

Versions:

Jira version: v7.13.8
SR version: 5.6.2

1 vote
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 25, 2018

Hello @d-sokolovskiy

This feature is now supported out of the box

https://scriptrunner.adaptavist.com/5.4.28/jira/releases/current-release.html

  • [SRJIRA-2668] - Add a Script Fields Template for Dates without timestamps
1 vote
MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 2018

Hello jiradev,

I think your only option is to implement Text script field and manually convert the date into the format you want. You can take a look at how to do it in this answer by Kristian from Adaptavist: https://community.atlassian.com/t5/Jira-questions/Custom-field-based-on-Priority-created-date/qaq-p/75549

Regards,
Martin

d-sokolovskiy January 5, 2018

Hi Martin,                                                                                                                     
Thank you for your answer. Everything works perfectly! But the problem is that the script field from the Dates section moved to the Details(text field section) on my screen. Do you have any ideas how can this be fixed?

Thanks.

MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2018

Hm, you are right. There is no control over where the fields are shown (Date ones on the right in Dates panel, People under People and so on). Making the field of text type unfortunately means it will be showed together with the other text ones. :(

Martin

David Sumlin January 5, 2018

It also means that it can't be searched/filtered in JQL using date functions or datepickers.

MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 6, 2018

That's true, I think the only option would be to implement a new custom field which would extend com.atlassian.jira.issue.customfields.impl.DateCFType  without any specific implementation. Only view and edit templates for this new custom field would be different - only date part could be edited and displayed. I wasn't able to find any regular documentation about writing new custom field types so it would expect some experience in using JIRA source code to learn how to do it.

Martin

David Sumlin January 6, 2018

It's aggravating since Jira already comes with the functionality we want out of the box for Due date (and I think a couple other fields).

This is completely a display/formatting issue.  We understand that there's a date component behind the scenes.  We also understand that the date could display different depending on what time zone the user is in. 

I'm actually surprised there's not an addon that has this functionality.  There's little chance that Atlassian will add this feature, since this has been a desired feature for years.

MoroSystems Support
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2018

+1 from me for this type of custom field:)

Sid September 5, 2018

Does anyone know how to clear the Due Date field value during cloning so that the new ticket does not have the old tickets' Due Date field pre populated while cloning?

I tried a post function but could not get the code right. 

Thanks

TAGS
AUG Leaders

Atlassian Community Events