The code provided below can be used for the endpoint to return a date by adding a specific number of days to the current day. The script excludes holidays and weekends when adding the days and considers the current date and time in the UK timezone.
/**
*@NApiVersion 2.1
*@NScriptType Suitelet
*/
define(['N/format', 'N/search'],
/**
* @param{format} format
* @param{search} search
*/
function (format, search) {
/**
* Defines the Suitelet script trigger point.
* @param {Object} scriptContext
* @param {ServerRequest} scriptContext.request - Incoming request
* @param {ServerResponse} scriptContext.response - Suitelet response
* @since 2015.2
*/
function onRequest(context) {
try {
if (context.request.method === 'GET') {
const exemptDayResult = search.load({
id: 'customsearch_exempt_days_search'
}).run().getRange({
start: 0,
end: 1000
});
let currentDate = new Date();
log.debug('currentDate', currentDate);
let londonDate = format.format({
value: currentDate,
type: format.Type.DATETIME,
timezone: format.Timezone.EUROPE_LONDON
});
log.debug('londonDate', londonDate);
let [datePart, timePart] = londonDate.split(' ');
let [day, month, year] = datePart.split('/');
let [time, period] = timePart.split(' ');
let [hours, minutes, seconds] = time.split(':');
hours = parseInt(hours, 10);
if (period === 'PM' && hours !== 12) {
hours += 12;
} else if (period === 'AM' && hours === 12) {
hours = 0;
}
let londonDateTest = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds));
log.debug('London Date Object', londonDateTest);
let endOfDay = new Date(londonDateTest.getTime());
endOfDay.setUTCHours(23, 59, 59, 999);
log.debug('End of Day', endOfDay);
let remainingTimeInMillis = endOfDay - londonDateTest;
log.debug('remainingTimeInMillis', remainingTimeInMillis);
let remainingTimeInSeconds = Math.floor(remainingTimeInMillis / 1000);
log.debug('Remaining Time in Seconds', remainingTimeInSeconds);
let result = {
currentDate: londonDate,
Standard_12pm_12pm: calculateRequiredDate(9, exemptDayResult),
Express_12am_3_30pm: calculateRequiredDate(4, exemptDayResult),
Express_3_30pm_12am: calculateRequiredDate(5, exemptDayResult),
Expiry_Time: remainingTimeInSeconds
};
log.debug('result', result);
context.response.write({
output: JSON.stringify(result)
});
}
} catch (e) {
log.error('error @ onRequest', e);
return false;
}
}
/**
* Calculates the required date by adding a specified number of days to the current date,
* excluding weekends and specified exempt days.
*
* @param {number} addDays - The number of days to add to the current date.
* @param {Array} exemptDayResult - Array of objects representing exempt days, with each object having a 'custrecord_exempt_day' property.
* @returns {string} - The calculated required date in 'MM/DD/YYYY' format.
*/
function calculateRequiredDate(addDays, exemptDayResult) {
try {
let currentDate = new Date();
let londonDate = format.format({
value: currentDate,
type: format.Type.DATETIME,
timezone: format.Timezone.EUROPE_LONDON
});
let londonDate_2 = format.parse({
value: londonDate,
type: format.Type.DATETIME,
timezone: format.Timezone.EUROPE_LONDON
});
let newDate = londonDate_2;
let daysAdded = 0;
while (daysAdded < addDays) {
newDate.setDate(newDate.getDate() + 1);
if (!isWeekend(newDate) && !isDateExempt(newDate, exemptDayResult)) {
daysAdded++;
}
}
while (isDateExempt(newDate, exemptDayResult)) {
newDate.setDate(newDate.getDate() + 1);
}
const formattedDate = newDate.toISOString().split('T')[0].split('-').reverse().join('/');
return formattedDate;
} catch (e) {
log.error('error @ calculateRequiredDate', e);
return false;
}
}
/**
* Checks if a given date is a weekend.
*
* @param {Date} date - The date to check.
* @returns {boolean} - True if the date is a weekend, false otherwise.
*/
function isWeekend(date) {
try {
const day = date.getUTCDay();
return day === 0 || day === 6;
} catch (e) {
log.error('error @ isWeekend', e);
return false;
}
}
/**
* Checks if a given date is an exempt day.
*
* @param {Date} date - The date to check.
* @param {Array} exemptDays - Array of objects representing exempt days, with each object having a 'custrecord_exempt_day' property.
* @returns {boolean} - True if the date is an exempt day, false otherwise.
*/
function isDateExempt(date, exemptDays) {
try {
let dateText = format.format({ value: date, type: format.Type.DATE });
for (let i = 0; i < exemptDays.length; i++) {
if (dateText === exemptDays[i].getValue('custrecord_exempt_day')) {
return true;
}
}
return false;
} catch (e) {
log.error('error @ isWeekend', e);
return false;
}
}
return {
onRequest: onRequest
};
});