It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to add days to a date field to populate another date field

Pavithra D Jul 18, 2016

Hi Experts,

I am trying to populate a Date field by Adding days to another date field value.

Consider, there are two date fields "Date1" & "Date2" and a numeric field "Number Field" for days.

Required Output : Date2 = Date1+NumberField

 How can I achieve this with the groovy script?

 

Regards,

Pavithra D

3 answers

1 accepted

3 votes
Answer accepted
Thanos Batagiannis [Adaptavist] Community Leader Jul 19, 2016

Hi Pavithra

You can use a scripted field for the Date2, which will be 'connected' to Date1 and the value of the number field. The script that calculates the Date2 will be something like

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

Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def daysToAddCF = customFieldManager.getCustomFieldObjectByName("Number Field")
def dateACF = customFieldManager.getCustomFieldObjectByName("Date A")

def dateAValue = issue.getCustomFieldValue(dateACF) as Date
def daysToAdd = issue.getCustomFieldValue(daysToAddCF) as int
def dateB = Calendar.getInstance()

dateB.setTime(dateAValue)
dateB.add(Calendar.DATE, daysToAdd)
return dateB.getTime()

So every time you change one of the Number Field or Date A fields the Date B field will be updated automatically. 

Regards

Thanos

Pavithra D Jul 19, 2016

Hi Thanos,

This worked perfectly.

Thanks a lot. 

Regards,

Pavithra D

Pavithra D Dec 13, 2016

Hi,

 

In the date time script field how to truncate the time? And is there any way to do this in the normal date field?

 

Thanks,

Pavithra D

Brian Alden Jan 27, 2017

Is there a way to subtract instead of add?

"dateB.subtract(Calendar.DATE, daystoAdd)" did not work for me.

0 votes
flan elflani Sep 11, 2017

Hi,

I tried to adjust the code you posted for my specific requirement which is to have a static number instead of number Custom field but I am getting 'Invalid Date' in the field where the result should be. Could you please have a look at the code and tell me where the problem is?

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

Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
int daysToAdd = 10
def FirstDate = customFieldManager.getCustomFieldObjectByName("FirstDate")

def dateAValue = issue.getCustomFieldValue(FirstDate) as Date
def Calculated = Calendar.getInstance()
Calculated.setTime(dateAValue)
Calculated.add(Calendar.DATE, daysToAdd)
return Calculated.getTime()

Thanks,

Flan 

Thanos Batagiannis [Adaptavist] Community Leader Sep 11, 2017 • edited

Hi Flan,

It should have just worked. 

What kind of searchers and tempate you use for the scripted field ?

flan elflani Sep 11, 2017

Hi Thanos, 

Searcher: Free Text Searcher

Template: Date Time Picker

Thanks, 

Flan

Thanos Batagiannis [Adaptavist] Community Leader Sep 12, 2017

Hey Flan,

Try with a date picker searcher instead. 

regards Thanos

flan elflani Sep 12, 2017

It worked perfectly! Thank you very much Thanos :) 

Another question please: Is it possible to do that with months?

i.e. Datefield2 = Datefield1 - 10 months

This is the code I am trying to do that with (not working at all):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import java.text.DateFormat
import groovy.time.TimeCategory
import java.util.Date

Issue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def FDate = customFieldManager.getCustomFieldObjectByName("FirstDate")

def dateAValue = issue.getCustomFieldValue(FDate) as Date
def Calculated = Calendar.getInstance()
def tDate = new Date()
use (groovy.time.TimeCategory){
tDate = dateAValue - 10.months
    Calculated.setTime(tDate)}

return Calculated.getTime()

Thank you again in advance,

Flan

0 votes
Sonya S. Dec 12, 2017

it looks like "Jira Misc Custom Fields" this add one can easy to do the calculation and doesn't need to save the value in DB. but does any know how to create the code?
thanks.

https://marketplace.atlassian.com/plugins/com.innovalog.jmcf.jira-misc-custom-fields/server/support

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

1,095 views 13 15
Read article

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