SuiteScript, the scripting language used in NetSuite, offers robust functionalities for date manipulation through modules like ‘N/format.’ Two essential methods provided by this module, format.parse(options) and format.format(options), play a pivotal role in parsing and formatting date values to and from their raw representations. This article explores the syntax and usage of these methods for effective date handling in SuiteScript.
Parsing Dates with format.parse(options)
The format.parse(options) method is instrumental in parsing a value from the appropriate preference format to its raw representation. The preference format is determined by the Date Format selected in the user’s preferences. When applied to datetime or datetimetz values, it returns a Date Object.
// ‘N/format’ module is utilized
let parseDate = format.parse({
type: format.Type.DATE,
value: date
});
Example Usage:
Suppose the preferred date format is “2023-12-21 (yyyy-MM-dd)” or “21 December, 2023 (DD Month, yyyy).” In such cases, format.parse() ensures a consistent output, converting the date to its raw representation as “2023-12-21T08:00:00.000Z.” To extract specific components like day, month, or year, you can subsequently use getDate(), getMonth(), or getFullYear() on the obtained Date Object.
Formatting Dates with format.format(options)
Conversely, format.format(options) is employed to format a value from its raw representation to the appropriate preference format. If a datetime or datetimetz value is specified, the resulting string is presented in the user’s local app time zone.
// ‘N/format’ module is utilized
let formatedDate = format.format({
type: format.Type.DATE,
value: newDate
});
Example Usage:
Assume the date format used in the NetSuite account is “DD Month, yyyy.” Utilizing format.format(), you can format a given date object (newDate) into the specified NetSuite format. For instance, it will return “21 December, 2023” for the input “2023-12-21T08:00:00.000Z.”
These methods are your allies for smooth date handling in SuiteScript. Whether you’re parsing dates from records or formatting date objects, format.parse() and format.format() ensure consistency and precision, making your date-related operations in SuiteScript more reliable