Skip to Content

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 com.sap.it.api.mapping.*;
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.

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Posted on Jul 14, 2020 at 05:57 PM

    hi Akash Shrivastava,

    The scope of duration variable is not outside where you are returning.

    Either use return inside use block or define duration outside use block.

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

    Hope this helps !

    thanks and regards,

    Praveen T

    Add a comment
    10|10000 characters needed characters exceeded