format to dateTime object using moment.js

define([ ‘N/format’, ‘N/runtime’, ‘./moment’],

    /**

     *

     * @param {*https} https

     * @param {*https} https

     * @param {*format} format

     * @param {*search} search

     * @param {*config} config

     * @param {*jjConstants} jjConstants

     * @param {*moment} moment

     * @returns

    */

    ( format, , runtime, moment) => {   

    /**

        * Converts a given date string into a 12-hour formatted date-time string.

        * The function parses the input date in various formats and returns it in the format: “DD-MMM-YYYY h:mm:ss am/pm”.

        *

        * @param {string} dateStr – The input date string to be converted. It should be a valid date-time string that can be parsed by NetSuite’s format module (e.g., “08/04/2025 01:20:00 PM”).

        * @param {string} dateFormat – The format of the input date string. It can be either “YYYY-MM-DD h:mm:ss” or “dd/MM/yyyy hh:mm:ss A”.

        * @returns {object} – The formatted date string in the format “DD-MMM-YYYY h:mm:ss am/pm”.

        *                     If the conversion fails, it returns an empty string.

        * input: “2025-04-08 21:20:00” || “08/04/2025 01:20:00 PM”

        * OutPut: ‘Sun Apr 06 2025 08:53:00 GMT-0700 (PDT)’

        *

        *

        * @throws {Error} If the date parsing or formatting fails, an error will be logged, and an empty string will be returned.

        */

        function convertTo12HourFormat(dateStr1, dateFormat) {

            try {

                let currentUser = runtime.getCurrentUser();

                let userDateFormat = currentUser.getPreference({ name: ‘DATEFORMAT’ });

                let userTimeFormat = currentUser.getPreference({ name: ‘TIMEFORMAT’ });

                // Define mapping of NetSuite to Moment.js formats for Month formats only

                let netsuiteToMomentDateFormats = {

                    ‘DD-Mon-YYYY’: ‘DD-MMM-YYYY’,

                    ‘D-Mon-YYYY’: ‘D-MMM-YYYY’,

                    ‘DD-MONTH-YYYY’: ‘DD-MMMM-YYYY’,

                    ‘D-MONTH-YYYY’: ‘D-MMMM-YYYY’,

                    ‘D MONTH, YYYY’: ‘D MMMM, YYYY’,

                    ‘DD MONTH, YYYY’: ‘DD MMMM, YYYY’,

                };

                let momentDateFormat = netsuiteToMomentDateFormats[userDateFormat] || userDateFormat;

                let formattedDate = moment(dateStr1, dateFormat || ‘YYYY-MM-DD h:mm:ss’).format(momentDateFormat + ‘ ‘ + userTimeFormat);

                let formattedDateObj = format.parse({

                    value: formattedDate,

                    type: format.Type.DATETIMETZ,

                    timezone: format.Timezone.ASIA_CALCUTTA

                });

                return formattedDateObj;

            } catch (e) {

                log.error(‘Error @ convertTo12HourFormat’, e);

                return {};

            }

        }

});

Leave a comment

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