N/format Module Script Samples

The following script samples demonstrate how to use the features of the N/format module:

Parse a String to a Date Object

The following sample parses a string (formatted according to the user’s preferences) to a raw Date object, and then parses it back to the formatted string. This sample uses format.parse(options) and format.format(options).

This sample assumes the Date Format set in the preferences is MM/DD/YYYY. You may need to change the value for the date used in this script to match the preferences set in your account.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/ui/serverWidget', 'N/format'], function(serverWidget, format) {
    function parseAndFormatDateString() {
        // Assuming Date format is MM/DD/YYYY
        var initialFormattedDateString = "07/28/2015";
        var parsedDateStringAsRawDateObject = format.parse({
            value: initialFormattedDateString,
            type: format.Type.DATE
        });
        var formattedDateString = format.format({
            value: parsedDateStringAsRawDateObject,
            type: format.Type.DATE
        });
        return [parsedDateStringAsRawDateObject, formattedDateString];
    }
    function onRequest(context) {
        var data = parseAndFormatDateString();

        var form = serverWidget.createForm({
            title: "Date"
        });

        var fldDate = form.addField({
            type: serverWidget.FieldType.DATE,
            id: "date",
            label: "Date"
        });
        fldDate.defaultValue = data[0];

        var fldString = form.addField({
            type: serverWidget.FieldType.TEXT,
            id: "dateastext",
            label: "Date as text"
        });
        fldString.defaultValue = data[1];

        context.response.writePage(form);
    }
    return {
        onRequest: onRequest
    };
}); 

          

Parse a String to a Number

The following sample parses a string (formatted according to the user’s preference) to a raw number value, using format.parse(options).

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/format'], 
    function(format){
         function parseToValue() {
              // Assume number format is 1.000.000,00 and negative format is -100
              var formattedNum = "-20.000,25"
              return format.parse({value:formattedNum, type: format.Type.FLOAT})
              }
         var rawNum = parseToValue(); // -20000.25 -- a number 
   }); 

          

Format a Number as a String

The following sample formats a raw number value (formatted according to the user’s preference) as a string using format.format(options).

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/format'], 
    function(format){
         function formatToString() {
              // Assume number format is 1.000.000,00 and negative format is (100)
              var rawNum2 =  -44444.44
              return format.format({value:rawNum2, type: format.Type.FLOAT}) 
              }
         var formattedNum2 = formatToString(); // "44.444,44" -- a string
   }); 

          

Format Time of Day as a String

The following sample formats the time of day as a string using format.format(options).

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

            /**
 * @NApiVersion 2.x
 */

require(['N/format'], 
    function(format){
         function formatTimeOfDay() {
              // Assume the time format is hh:mm (24 hours) 
              var now = new Date(); // Say it's 7:01PM right now.
              return format.format({value: now, type: format.Type.TIMEOFDAY})  
              }
        var formattedTime = formatTimeOfDay(); // "19:01" -- a string 
  }); 

          

General Notices