To find the Days difference between the two Dates

When we need to find the days difference between two dates use the below-added code.

  • Add the configuration module to the script
    var companyInfo = config.load({
                      type: config.Type.COMPANY_INFORMATION
                      });
       var TIMEZONE = companyInfo.getValue({
                      fieldId: 'timezone'
                      });                  // to get the time zone of the netSuite account.
      var todayDate = new Date();   
       // today's date
var formatTodayDate = format.format({
                      value: todayDate,
                      type: format.Type.DATE,
                      timezone: format.Timezone.TIMEZONE
                      });                  // format 'todayDate'
var formatTodayDateObj = format.parse({
                      value: formatTodayDate ,
                      type: format.Type.DATE
                      });
  var referenceDate = format.parse({
                      value: formValues.date,
                      type: format.Type.DATE
                      });
  var timeDifference = formatTodayDateObj .getTime() - referenceDate.getTime();
var differenceInDays = Math.ceil(timeDifference / (1000 * 3600 * 24));

The comparing date should be in the same format.

Here if ‘todayDate’ is 30/05/2023, and the ‘referenceDate’ is 28/05/2023, then the ‘differenceInDays’ value will be ‘2’.

If ‘todayDate’ is 28/05/2023, and the ‘referenceDate’ is 30/05/2023, then ‘differenceInDays’ value will be ‘-2’.

Leave a comment

Your email address will not be published. Required fields are marked *