DateTime Formatting in Suite Script

This article introduces a simple yet effective function, formatDateTime, which streamlines the dateTime formatting process in SuiteScript based on the format in netsuite.

formatDateTime(dateTime) {
    let [datePart, timePart] = dateTime.split(" ");
    let [year, month, day] = datePart.split("-");
    let [hours, minutes, seconds] = timePart.split(":");
    let newDate = new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds));


    let formattedDateTime = format.format({
        value: newDate,
        type: format.Type.DATETIMETZ,
        timezone: 'GMT'
    });
    return formattedDateTime;
}

Explanation:

  • The formatDateTime function takes a dateTime string as input.
  • It splits the dateTime string into datePart and timePart based on the space delimiter.
  • Then, it further splits the datePart into year, month, and day components using the hyphen delimiter.
  • Similarly, the timePart is split into hours, minutes, and seconds based on the colon delimiter.
  • Using the extracted date and time components, a new Date object is created with UTC time zone using Date.UTC.
  • The newDate object is formatted into a string representing the date and time in GMT time zone using format.format with type DATETIMETZ.
  • Finally, the formatted date and time string is returned.

Example Usage:

let formattedDate = formatDateTime('2024-05-16 09:05:01');

Conclusion:

The formatDateTime function provides a concise and efficient solution for formatting dates times in SuiteScript based on the format in NetSuite.

Leave a comment

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