Wednesday, April 24, 2019

Script to Calculate Time Duration between Two Fields Type "Time Of Day"

Sample script below shows how to calculate time duration between two fields type "Time Of Day".

Function timeToMinutes will convert time for each field to minutes according to set time format. If time contains "pm" there is an added 720 minutes (12 hours).

This function is called for start time and end time field. If start time in minutes is higher than end time, it means that end time was next day. In this case it is added 24 hours to end time. Therefore, maximum duration limit is 23 hours and 59 minutes.

At the end of the script, it will set the calculated duration (hours and minutes) into result field. If the user uses a 24 hour format, this result field can be also "Time Of Day" type. If the user uses am/pm format, the result field should be "Free-Form Text" type because  duration doesn't need to have a period. For example, if the duration is 14:35, it is not appropriate to have 2:35 pm displayed there.



Script Sample

function timeToMinutes(time) {
  var minutes = 0;
  var splitSign = "";
  // load the user preferences page
  var userPreferences = nlapiLoadConfiguration('userpreferences');
  // get time and date format
  var timeFormat = userPreferences.getFieldValue('TIMEFORMAT');
  if ( (timeFormat == "fmHH:fmMI am") || (timeFormat == "fmHH-fmMI am") ) {
    // separate am/pm sign
    var timeFieldValue = time.split(" ");
    // choose split sign
    if (timeFormat == "fmHH:fmMI am") {splitSign = ":";} else {splitSign = "-";}
    // split hours and minutes
    var timeArray = timeFieldValue[0].split(splitSign);
    // count minutes (hours * 60) + minutes
    minutes = (parseInt(timeArray[0]) * 60) + parseInt(timeArray[1]);
    // add 12 hours if there is pm
    if (timeFieldValue[1] == "pm") {minutes += 720;}
  }else{
    // 24 hours time format (no am/pm)
    //choose split sign
   if (timeFormat == "fmHH24:fmMI") {splitSign = ":";} else {splitSign = "-";}
   // split hours and minutes
    var timeArray = time.split(splitSign);
    // count minutes (hours * 60) + minutes
   minutes = (parseInt(timeArray[0]) * 60) + parseInt(timeArray[1]);
}
  return minutes;
} // end of function timeToMinutes

// load start time and end time field values
var startTime = nlapiGetFieldValue('id_of_start_time_field');
var endTime = nlapiGetFieldValue('id_of_end_time_field');
// convert both to minutes
var startTimeMinutes = timeToMinutes(startTime);
var endTimeMinutes = timeToMinutes(endTime);
// if end time is next day - add 24hours to end time
if (endTimeMinutes < startTimeMinutes) { endTimeMinutes += 1440; }
// calculate duration and convert it to hours and minutes
var duration = endTimeMinutes - startTimeMinutes;
var durationMinutes = duration % 60;
// if there is less than 10 minutes in result -> add a 0 before to have 01 instead of 1 minutes.
if (durationMinutes < 10) {
  durationMinutes.toString();
  durationMinutes = "0" + durationMinutes;
}
var durationHours = Math.floor(duration / 60);
// load the user preferences page
var userPreferences = nlapiLoadConfiguration('userpreferences');
// get time and date format
var timeFormat = userPreferences.getFieldValue('TIMEFORMAT');
if ( (timeFormat == "fmHH:fmMI am") || (timeFormat == "fmHH24:fmMI") ) {
  var resultDuration = durationHours + ":" + durationMinutes;
}else{
  var resultDuration = durationHours + "-" + durationMinutes;
}
nlapiSetFieldValue('id_of_result_field',resultDuration);


DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.


Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.


Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.

No comments:

Post a Comment