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

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
Accepted answer

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

Hi Thanos,

This worked perfectly.

Thanks a lot. 

Regards,

Pavithra D

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

Is there a way to subtract instead of add?

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

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 

Hi Flan,

It should have just worked. 

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

Hi Thanos, 

Searcher: Free Text Searcher

Template: Date Time Picker

Thanks, 

Flan

Hey Flan,

Try with a date picker searcher instead. 

regards Thanos

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

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
Community showcase
Published yesterday in Feedback & Announcements

How do you analyze GC logs, thread dumps and head dumps?

Hi awesome community!  In this article, I would like to describe the one of the toolset (service) for the analyze some problems on different Java-based instances, of course, as Atlassian admini...

93 views 0 7
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you