Monday, October 8, 2018

How to get the difference between two dates in Days, Hours and Minutes using scripting

Question:
 
User needs to get the difference between two date fields using scripts. The difference should be in Minutes, Hours and Days.
 

Answer:
 
function getDifferenceBetweenDates() {
 
      var opportunityId = 1;
      var fields = ['closedate', 'datecreated'];
      var columns = nlapiLookupField('opportunity', opportunityId, fields);
     
      var createdDate = new Date(columns.datecreated);
      var dateClosed = new Date(columns.closedate);
     
      var differenceInMinutes = (dateClosed.getTime() - createdDate.getTime()) / (1000 * 60);
      var differenceInHours = (dateClosed.getTime() - createdDate.getTime()) / (1000 * 60 * 60);
      var differenceInDays = (dateClosed.getTime() - createdDate.getTime()) / (1000 * 60 * 60 * 24);
     
      nlapiLogExecution('DEBUG', 'Difference in Minutes', differenceInMinutes);
      nlapiLogExecution('DEBUG', 'Difference in Hours', differenceInHours);
      nlapiLogExecution('DEBUG', 'Difference in Days', differenceInDays);
     
}
 
Notes:
 
1. The above function illustrates how to get the Date difference between to Dates.
 
2. Modify the function to make it dynamic and to fit your process.

No comments:

Post a Comment