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.ComponentAccessordef issue = Issues.getByKey('PROJECT-xxxx')String customFieldName = "Target end"def changeHistoryManager = ComponentAccessor.changeHistoryManagerdef changeHistory = changeHistoryManager.getChangeItemsForField(issue, customFieldName)def oldDate = changeHistory.last().fromdef newDate = changeHistory.last().to//def n_day = newDate - oldDate
Any help is important.
Thank you .
Best regards, Alex!
Hi @Alex
You can refer to this example.
https://library.adaptavist.com/entity/calculate-difference-between-dates
Regards
@Florian Bonniec Hi! Thank you for answer ! This Is helpfull!
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.
import com.atlassian.jira.component.ComponentAccessorimport java.text.SimpleDateFormatimport java.util.Dateimport java.time.temporal.ChronoUnitdef issue = Issues.getByKey('PROJECT-xxxx')String customFieldName = "Target end" //your cf datedef changeHistoryManager = ComponentAccessor.changeHistoryManagerdef changeHistory = changeHistoryManager.getChangeItemsForField(issue, customFieldName)def oldDate = changeHistory.last().fromdef newDate = changeHistory.last().to String strDateOld = oldDateString strDateNew = newDateSimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");Date date_old = format.parse(strDateOld)Date date_new = format.parse(strDateNew)// Transform both values to instantsdef 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
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.ZonedDateTimeimport java.time.format.DateTimeFormatterimport java.time.temporal.ChronoUnitimport java.time.ZoneId// Define the issue key and the custom field IDs for start date and end datefinal String issueKey = issue.keyfinal String startDateFieldId = "customfield_10037" // Replace with the actual ID of your start date fieldfinal String endDateFieldId = "customfield_10038" // Replace with the actual ID of your end date field// Define the temporal unit for calculationfinal ChronoUnit chronoUnit = ChronoUnit.DAYS// Define the date format expected in the custom fieldsfinal DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")// Get the values of the custom date fields from the issuedef startDateString = issue.fields[startDateFieldId] ?: ""def endDateString = issue.fields[endDateFieldId] ?: ""// Parse the dates if they are not emptyZonedDateTime startDate = startDateString ? ZonedDateTime.parse(startDateString + 'T00:00:00Z') : nullZonedDateTime endDate = endDateString ? ZonedDateTime.parse(endDateString + 'T00:00:00Z') : null// Calculate the difference between the dates if they are not nulldef dateDifference = nullif (startDate && endDate) {dateDifference = chronoUnit.between(startDate, endDate) as Integer}
It looks like you're new here. Sign in or register to get started.