I'm trying to add 2 business days to a date (trand...
# suitescript
m
I'm trying to add 2 business days to a date (trandate) and put the result in a Date/Time custom field. The time needs to be always set to 12:00 pm. I'm using moment.js to add the 2 business days (skipping weekends), but I'm having trouble setting the time to 12:00 pm and then inserting into the custom field. I'm open to other ways of doing this.
var dateStr, futureDate, currentDayOfWeek, numberOfWeekends, tString, nString;
            
var time, timeStr, localFDate;
            
try{
                
log.debug(func, "Starting: " + JSON.stringify({
                    
originalDate: originalDate,
                    
numberOfWeekdays: numberOfWeekdays
                
}));
                
futureDate = moment(originalDate);
                
currentDayOfWeek = futureDate.day();            // 0 = Sunday, 1 = Monday, ..., 6 = Saturday
                
log.debug(func, "first calc: " + JSON.stringify({ futureDate: futureDate, currentDayOfWeek: currentDayOfWeek }));
                
numberOfWeekends = Math.floor((currentDayOfWeek + numberOfWeekdays - 1) / 5);   // calculate the number of weekends to skip over
                
futureDate.add(numberOfWeekdays + numberOfWeekends * 2, 'days');    // account for the 2 days per weekend
                
log.debug(func, "second calc: " + JSON.stringify({ numberOfWeekends: numberOfWeekends, futureDate: futureDate }));
                
// attempting to set the time to 12:00 pm
                
timeStr = '12:00';
                
time    = moment(timeStr, 'HH:mm');
                
futureDate.set({
                    
hour:   time.get('hour'),
                    
minute: time.get('minute'),
                    
second: time.get('second')
                
});
                
localFDate = moment.utc(futureDate).local().format();
                
log.debug(func, "third calc: " + JSON.stringify({ futureDate: futureDate, localFDate: localFDate }));
                
dateStr = format.format({value: localFDate, type: format.Type.DATETIME});
                
log.debug(func, "Returning dateStr: " + dateStr);
                
return dateStr;
s
I don't see where you are setting the field value, but using
moment.format()
in combination with
field.setText()
should be pretty straight forward.
But as @scottvonduhnalready pointed out, timezones make 12:00 ambiguous
let twoDaysLaterAt12 = moment().startOf('day').add(2, 'days').add(12, 'hours').format('L HH:mm');
should be pretty close to a format setText on a date time would take
s
@Sandii Yep, though
startOf('day')
will always be midnight with respect to the current timezone, which by default will be the NetSuite server's (probably Pacific time).
s
You could control the timezone of the moment object with
.UtcOffset()
, if you care about that
b