Skip to Content
Jul 14, 2020 at 05:51 PM

Groovy script to calculate number of days between two dates in cpi


Hi Experts,

I have a requirement to get the number of days between two dates using groovy script in cpi.

I am using the below script and it is giving me some error.

import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import groovy.time.TimeCategory;

def String customFunc(String startDate, String endDate){
    def pattern = "yyyy-MM-dd";
def startDateF = new SimpleDateFormat(pattern).parse(startDate);
def endDateF = new SimpleDateFormat(pattern).parse(endDate);

    use(groovy.time.TimeCategory) {
    def duration = (startDateF - endDateF).days;
	return duration; 

Can you please help me correct the script to the find the number of days between two dates.