Need to Calculate a Date Based on a Start Date? Read on for an Example Using Suitescript.

One of the more common requests from NetSuite customers is to set an end date based on a given start date and a schedule. Often, these requests are for transaction line level items to set a billing schedule for particular items. If you require a dynamic method to set a future date based on a start date, there are scripting options in NetSuite to provide this functionality. Below we will look at some examples.

Scenario 1 – Start and End Months with Same Number of Days:

  1. Start Date: December 31.
  2. Months to Add: 1
  3. Result: January 31.

Scenario 2 – Start Month Containing Fewer Days than End Month:

  1. Start Date: February 28.
  2. Months to Add: 1
  3. Result: March 28

Scenario 3 – Start Month Containing More Days the End Month:

  1. Start Date: January 31.
  2. Months to Add: 1
  3. Result: February 28.

Scenario 4 – Start Month Containing Fewer Days than End Month – Force End Month Date to be End of Month if Start Date is Last Day of Month:

  1. Start Date: February 28 (Last Day of Month)
  2. Months to Add: 1
  3. Result: March 31.

The Months to Add variable in these scenarios can be set to any value, including dynamic values based on record data. For example, some records may require adding 3, 6, or 12 months to the start date.

Using date libraries such as Date and Moment.js, we are able to accomplish these requirements.

Below is an example using the Moment.js library to set an end date based on a given start date and a 3 month interval:

// Set Initial End Date to Start Date Using Moment
let endDate = new moment(startDate);

// Add Number of Months to Date
endDate.add(3, ‘month’);

// Convert back to Javascript Date Object.
endDate = new Date(endDate);

Have additional questions regarding automation? Contact us using the form below!

Related Posts