Convert date into specific format

Using this function, we can convert to date into defined format. Define the function in the script.

function generateDateString(dateObj) {

            //convert date to specific format

            var date = dateObj;

            log.debug(“date”, date)

            var d = date.getDate();

            log.debug(“d”, d);

            var m = date.getMonth() + 1;

            log.debug(“Month: “, m)

            var y = date.getFullYear()

            log.debug(“Year: “, y)

            var dateString = ((d <= 9 ? ‘0’ + d : d) + ‘/’ + (m <= 9 ? ‘0’ + m : m) + ‘/’ + y);

            log.debug(“datestring: “, dateString);

            return dateString;

        }

Read the date from the system and pass the date as a argument in the function. In the function, we can set the specific format of date.

  const doc_date = customRec.getValue({
    fieldId: ‘trandate’,
    })
   log.debug(“doc_date”, doc_date)
   var date1 = generateDateString(doc_date)
   log.debug(“date1”,date1)

Leave a comment

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