ANS-1402 · SUITESCRIPT DEVELOPMENT
Resolving ‘NaN/NaN/NaN’ Date Errors in NetSuite Server-Side Scripts
Understand why the native JavaScript Date object constructor may produce 'NaN/NaN/NaN' errors when parsing specific date string formats in NetSuite server-side SuiteScripts, and learn best practices for robust date handling.
Short answer
The native JavaScript Date object constructor can fail to parse certain date string formats when executed in NetSuite's server-side JavaScript engine, resulting in 'NaN/NaN/NaN' errors. To resolve this, either pre-process the date string to a compatible format or, for robust and modern SuiteScript 2.x development, utilize the N/format module's parse() function for reliable date string conversion.
Scenario
A NetSuite script attempts to retrieve a date from a custom record, where the date is stored as a string (e.g., 'Wed Mar 25 2015 15:16:58 GMT-0400 UTC'). The script then tries to use this string to initialize a JavaScript Date object, intending to set it as the transaction date for new invoices. While this approach might function correctly in client-side scripts, server-side execution leads to an error, with logs indicating an attempt to set the transaction date as 'NaN/NaN/NaN'. This occurs even when attempting to convert the Date object to a string using functions like nlapiDateToString (a SuiteScript 1.0 API) before setting the field. The core issue lies with the Date object constructor's inability to parse the specific string format server-side.
Solution
The error 'NaN/NaN/NaN' when attempting to set a transaction date in a NetSuite server-side script indicates that the native JavaScript Date object constructor (new Date(dateString)) is unable to correctly parse the provided date string format. This discrepancy between client-side and server-side behavior is due to differences in how the JavaScript engine handles certain date string formats in the NetSuite server environment. The problematic line of code is:
javascript
var date1 new Date('Wed Mar 25 2015 15:16:58 GMT-0400 UTC');One approach to resolve this issue, as identified in earlier SuiteScript development, involves pre-processing the date string. This typically means 'chopping off some unneeded characters' from the original date string to transform it into a format that the native Date object constructor can reliably parse server-side. The exact string manipulation required depends on the specific format of the input date string.
For robust date string parsing and formatting in modern SuiteScript 2.x, the recommended best practice is to utilize the N/format module. This module provides reliable functions for converting strings to Date objects and vice-versa, handling various date and time formats consistently across NetSuite environments. While N/format.parse() is highly effective for standard NetSuite date formats (e.g., format.Type.DATE, format.Type.DATETIME), for highly custom or complex date strings like 'Wed Mar 25 2015 15:16:58 GMT-0400 UTC', you may still need to pre-process the string into a more standard format before using format.parse(), or consider if the original string manipulation workaround for the native Date object is more suitable for your specific custom string.
To convert a date string to a Date object and back to a formatted string using N/format:
Load the N/format module in your SuiteScript 2.x script.
Use format.parse() to convert the string to a Date object.
Use format.format() to convert the Date object to a string in a desired NetSuite format.
Example of using N/format for date handling:
javascript
/**
* @NApiVersion 2.x
* @NModuleScope SameAccount
*/
define(['N/format'], function(format) {
function handleDateConversion() {
// Example: A date string in a common NetSuite format
var standardDateString = '3/25/2015';
//
1. Parse the string into a Date object
var dateObject = format.parse({
value: standardDateString,
type: format.Type.DATE
});
// dateObject is now a native JavaScript Date object (e.g., Wed Mar 25 2015 00:00:00 GMT-0400 (EDT))
//
2. Format the Date object back into a string for a NetSuite field
var formattedStringForField = format.format({
value: dateObject,
type: format.Type.DATE
});
// formattedStringForField is now '3/25/2015' (or locale equivalent)
return formattedStringForField;
}
return {
handleDateConversion: handleDateConversion
};
});Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
