How to get the difference between two dates?(scriptrunner)

Alex
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 29, 2024

Hi,everyone!

 

Please tell me how I can get the number of days (difference) between the old date value and the new one.I started writing code but I'm having trouble getting the date.They are not deductible.

 

import com.atlassian.jira.component.ComponentAccessor

def issue = Issues.getByKey('PROJECT-xxxx')
String customFieldName = "Target end"

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def changeHistory = changeHistoryManager.getChangeItemsForField(issue, customFieldName)

def oldDate = changeHistory.last().from
def newDate = changeHistory.last().to

//def n_day = newDate - oldDate

Any help is important.

Thank you .

Best regards, Alex!

3 answers

1 accepted

1 vote
Answer accepted
Florian Bonniec
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 29, 2024

Hi @Alex 

 

You can refer to this example.

https://library.adaptavist.com/entity/calculate-difference-between-dates

 

Regards

Alex
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 30, 2024

@Florian Bonniec Hi! Thank you for answer ! This Is helpfull!

0 votes
Jose
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 8, 2024

If you are trying to implement this as a scripted field, this worked for me. Running Jira ScriptRunner cloud version. Just go to Scripted Fields, create a field, drop this code below and replace my custom ID numbers with yours. NOTE: This only takes custom ID numbers... not names of fields. Credits to Chat GPT for iterating through the errors with me until I finally got it to spit out the right answer. 

import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.time.ZoneId

// Define the issue key and the custom field IDs for start date and end date
final String issueKey = issue.key
final String startDateFieldId = "customfield_10037" // Replace with the actual ID of your start date field
final String endDateFieldId = "customfield_10038" // Replace with the actual ID of your end date field

// Define the temporal unit for calculation
final ChronoUnit chronoUnit = ChronoUnit.DAYS

// Define the date format expected in the custom fields
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")

// Get the values of the custom date fields from the issue
def startDateString = issue.fields[startDateFieldId] ?: ""
def endDateString = issue.fields[endDateFieldId] ?: ""

// Parse the dates if they are not empty
ZonedDateTime startDate = startDateString ? ZonedDateTime.parse(startDateString + 'T00:00:00Z') : null
ZonedDateTime endDate = endDateString ? ZonedDateTime.parse(endDateString + 'T00:00:00Z') : null

// Calculate the difference between the dates if they are not null
def dateDifference = null
if (startDate && endDate) {
dateDifference = chronoUnit.between(startDate, endDate) as Integer
}

 

0 votes
Alex
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 30, 2024

My final code with the condition that I also had to work with date conversion.

Because I compared the old and new values obtained from "changelog" , and there the date is in string format. I

hope this helps someone.

Best regards, Alex!

 

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat
import java.util.Date
import java.time.temporal.ChronoUnit

def issue = Issues.getByKey('PROJECT-xxxx')
String customFieldName = "Target end" //your cf date

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def changeHistory = changeHistoryManager.getChangeItemsForField(issue, customFieldName)

def oldDate = changeHistory.last().from
def newDate = changeHistory.last().to

String strDateOld = oldDate
String strDateNew = newDate
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date_old = format.parse(strDateOld)
Date date_new = format.parse(strDateNew)

// Transform both values to instants

def oldDateInstant = date_old?.toInstant()
def newDateInstant = date_new?.toInstant()

// Change the chrono unit to obtain the difference in other time unit.

def
chronoUnit = ChronoUnit.DAYS

// Calculate the difference between the lower and the higher date.

oldDateInstant && newDateInstant ? chronoUnit.between(oldDateInstant, newDateInstant) : null

Suggest an answer

Log in or Sign up to answer