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
formatDateTimefunction takes adateTimestring as input. - It splits the
dateTimestring intodatePartandtimePartbased on the space delimiter. - Then, it further splits the
datePartintoyear,month, anddaycomponents using the hyphen delimiter. - Similarly, the
timePartis split intohours,minutes, andsecondsbased on the colon delimiter. - Using the extracted date and time components, a new
Dateobject is created with UTC time zone usingDate.UTC. - The
newDateobject is formatted into a string representing the date and time in GMT time zone usingformat.formatwith typeDATETIMETZ. - 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.