NetSuite SuiteScript Functions
Here are a few simple functions that you can add to a script library. These are just a easy examples, but they may save you some time.
//==============================================================
//== START Formatting of Numbers
//===============================================================
/*
* Pass in a number and this function will insert commas in the
* appropriate places
*/
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
//=============================================================
// Get the current Month Name and Quarter
//=============================================================
function getDateParams(){
/*
* This function takes the current data
* and returns the abrev. Month Name
* and current Quarter
* Return: month name and quarter
*/
var dateArray=new Array();
var d = new Date();
var month = new Array();
month[0] = "jan";
month[1] = "feb";
month[2] = "mar";
month[3] = "apr";
month[4] = "may";
month[5] = "jun";
month[6] = "jul";
month[7] = "aug";
month[8] = "sep";
month[9] = "oct";
month[10] = "nov";
month[11] = "dec";
var m = month[d.getMonth()]; //Get the name of the current month
//** Get the Current Quarter
if(m=='jan'||m=='feb'||m=='mar')
{
qtr='Q1';
}
else if(m=='apr'||m=='may'||m=='jun')
{
qtr='Q2';
}
else if(m=='jul'||m=='aug'||m=='sep')
{
qtr='Q3';
}
else{
qtr='Q4'
}
dateArray[0]=m;
dateArray[1]=qtr;
return dateArray;
}
//****************************************************
// Pass a Date and an integer to add the number of days to the date
//*****************************************************
function addDays(theDate, days) {
return new Date(theDate.getTime() + days*24*60*60*1000);
}
//*************************************************************
//** FUNCTION: Working Days
//** Calculates the number of non-weekend days between 2 dates
//** If the dates are the same then 1 days is returned
//*************************************************************
function workingDaysBetweenDates(startDate, endDate) {
var dtstartDate=new Date(startDate);
var dtendDate=new Date(endDate);
// Validate input
if (dtendDate< dtstartDate)
return 0;
// Calculate days between dates
var millisecondsPerDay = 86400 * 1000;
// Day in milliseconds
dtstartDate.setHours(0,0,0,1);
// Start just after midnight
dtendDate.setHours(23,59,59,999);
// End just before midnight
var diff = dtendDate - dtstartDate;
// Milliseconds between datetime objects
var days = Math.ceil(diff / millisecondsPerDay);
// Subtract two weekend days for every week in between
var weeks = Math.floor(days / 7);
var days = days - (weeks * 2);
// Handle special cases
var startDay = dtstartDate.getDay();
var endDay = dtendDate.getDay();
// Remove weekend not previously removed.
if (startDay - endDay > 1){
days = days - 2;
}
// Remove start day if span starts on Sunday but ends before Saturday
if (startDay == 0 && endDay != 6){
days = days - 1;
}
// Remove end day if span ends on Saturday but starts after Sunday
if (endDay == 6 && startDay != 0) {
days = days - 1;
}
return days;
}