How to compare to dates?

Gordon Schmidt May 31, 2021

Hi Atlassian community,

I have a project where I want to compare to dates - a custom field with an "End date" and todays date.

I want to create another field (maybe supported by a behaviour) which, if todays date is after the "End date", gives me the text "Delayed" (otherwise "On Time").

Does anyone have a recommendation or a simple script that can achieve this?

Thank you very much!

Regards

Gordon

2 answers

0 votes
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 31, 2021

Hi @Gordon Schmidt  Welcome to Atlassian Community!

Try below code in behaviour of Script Runner to set Text field value based on comparion of end date and todays date. 

Kindly replace Testing time with End Date and Suggestion Text field name with your Text field name. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript
import java.time.LocalDate

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*

@BaseScript FieldBehaviours fieldBehaviours

// Set log level
def log = Logger.getLogger(getClass())
log.setLevel(Level.DEBUG)

//Date field which you want to compare with todays date
def cf = getFieldByName("Testing Time")
def cfval = cf.getValue() as Date

//Text field which will update based on End Date and Today date
def suggestiontext = getFieldByName("Suggestion Text")

//get today date
def today = new Date()


if (cfval.after(today)){
suggestiontext.setFormValue("Delayed")
}else {
suggestiontext.setFormValue("On Time")

}
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 31, 2021

Kindly arrange today and cfval as per your need in If statement. 

 

Thanks

V.Y

0 votes
Ioana
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.
May 31, 2021

Suggest an answer

Log in or Sign up to answer