NetSuite – SuiteScript 1.0 – Useful Functions

| |

These first function snippets are from https://gist.github.com/W3BGUY

// function to figure out the number of days in month.
function getDaysInMonth(thisMonth,thisYear){
  var monthArray=[31,28,31,30,31,30,31,31,30,31,30,31];
  if(thisMonth!=2){return monthArray[thisMonth-1]};
  if(thisYear%4!=0){return monthArray[1]};
  if(thisYear%100==0 && thisYear%400!=0){return monthArray[1]};
  return monthArray[1]+1;
}

// NetSuite usage example
nlapiLogExecution('DEBUG',getDaysInMonth(11,2016))
// function to add leading zeros on date parts.
function zeroPad(num,len){
  var str=num.toString();
  while(str.length<len){str='0'+str;}
  return str;
}

// function to format date object into NetSuite's mm/dd/yyyy HH:MM:SS format.
function formatNSDateTime(dateObj){
  if(dateObj){ 
    var nsFormatDateTime=(dateObj.getMonth()+1)+'/'+dateObj.getDate()+'/'+dateObj.getFullYear()+' '+addZeros(dateObj.getHours(),2)+':'+addZeros(dateObj.getMinutes(),2)+':'+addZeros(dateObj.getSeconds(),2);
    return nsFormatDateTime;
  }
  return null;
}

// example usage
var thisDateTime=formatNSDateTime(new Date());
// function to add leading zeros on date parts.
function zeroPad(num,len){
  var str=num.toString();
  while(str.length<len){str='0'+str;}
  return str;
}

// function to format date object into NetSuite's mm/dd/yyyy format.
function formatNSDate(dateObj){
  if(dateObj){ 
    var nsFormatDate=addZeros(dateObj.getMonth()+1,2)+'/'+addZeros(dateObj.getDate(),2)+'/'+dateObj.getFullYear();
    return nsFormatDate;
  }
  return null;
}

// example usage
var thisDate=formatNSDate(new Date());
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.