Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Pavithra D July 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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Answer accepted
Thanos Batagiannis _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.
July 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 July 19, 2016

Hi Thanos,

This worked perfectly.

Thanks a lot. 

Regards,

Pavithra D

Pavithra D December 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 January 27, 2017

Is there a way to subtract instead of add?

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

0 votes
Sonya S_ December 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

0 votes
flan elflani September 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_
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.
September 11, 2017

Hi Flan,

It should have just worked. 

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

flan elflani September 11, 2017

Hi Thanos, 

Searcher: Free Text Searcher

Template: Date Time Picker

Thanks, 

Flan

Thanos Batagiannis _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.
September 12, 2017

Hey Flan,

Try with a date picker searcher instead. 

regards Thanos

flan elflani September 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

TAGS
AUG Leaders

Atlassian Community Events