Converting Date to d/m/yyyy format

Scenario: Convert given date to the d/m/yyyy format

Solution:

You need to use the module ‘N/format‘ to convert the date to different formats. format.parse method can be use for this. It parses a value from the appropriate preference format to its raw value. The appropriate preference format is the one selected in the Date Format field at Home > Set Preferences. For a datetime or datetimetz value, use this method to convert a Date Object into a string based on the specified timezone.

The following is just an Example Syntax;


function getData(){
        var rec = record.load({
                         type: 'journalentry',
                         id: 2064, //You can change the record ID
                         isDynamic: true
                      })
       var transDate = rec.getValue({
                                fieldId: 'trandate'
                             })

       var initialFormattedDateString = convertDate(transDate);
       var parsedDateStringAsRawDateObject = format.parse({
                                        value: initialFormattedDateString,
                                        type: format.Type.DATE
                                    });
}

/**
         * Function to convert date to dateString format.
         * @param {Date} iDate - Date fieldValue
         * @returns {string} res - converted DateString
         */
        function convertDate(iDate){
            try {
                var d = new Date(iDate).getDate()
                switch (d) {
                    case '01' : d = '1'; break;
                    case '02' : d = '2'; break;
                    case '03' : d = '3'; break;
                    case '04' : d = '4'; break;
                    case '05' : d = '5'; break;
                    case '06' : d = '6'; break;
                    case '07' : d = '7'; break;
                    case '08' : d = '8'; break;
                    case '09' : d = '9'; break;
                    default: break;
                }
                var m = new Date(iDate).getMonth()+1
                var y = new Date(iDate).getFullYear()
                var res = d+'/'+m+'/'+y
                return res;
            }
            catch (e) {
                log.error({
                    title: "Error in Date function: ",
                    details: e.name+' : '+e.message
                })
            }
        }

/**
         * Function to convert dateString to date 'd/m/yyyy' format.
         * @returns {Date} convertedDate - converted Date
         */
function dateCreator(){
            try{

                //Date operations
                var d= new Date();
                var m;
                if(d.getMonth() == 11){
                    m= '1'
                }
                else{
                    m= d.getMonth()+2
                }
                var y = (d.getMonth()==11) ? (d.getFullYear()+1) : d.getFullYear()
                var dt = '1'+'/'+m+'/'+y
                var convertedDate = format.parse({
                    value: dt,
                    type: format.Type.DATE
                });
                return convertedDate

            }
            catch (e) {
                log.error({
                    title: "Error @ date Creator function: ",
                    details: e.name+' : '+e.message
                })
            }
        }

Leave a comment

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