Parsing and Formatting Dates in NetSuite Using the N/format Module

NetSuite’s N/format module provides useful functions for handling dates, making it easy to convert between string and date formats. The format.parseDate() function converts a date string into a JavaScript Date object based on the user’s date format preference. Conversely, format.formatDate() takes a Date object and converts it into a formatted string.

Example:

define(['N/format'], function(format) {
    function formatDateExample() {
        var date = format.parseDate({
            value: '12/31/2023',
            type: format.Type.DATE
        });
        log.debug('Parsed Date', date);


        var formattedDate = format.format({
            value: new Date(),
            type: format.Type.DATE
        });
        log.debug('Formatted Date', formattedDate);
    }
});

format.parseDate() converts a string like '12/31/2023' to a Date object.

format.formatDate() formats the current Date object into a string based on the user’s preferences.

These functions are useful for managing date conversions according to the user’s preferred date format.

Leave a comment

Your email address will not be published. Required fields are marked *