Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

You are asked to format the date and time in a specific format whenever a new record is inserted.

purnendutiw
Tera Contributor

I have one scenario , that i was practicing for :

You are asked to format the date and time in a specific format whenever a new record is inserted.
Ø Write a Script Include that accepts a date as input and formats it to DD/MM/YYYY HH:MM AM/PM format.

 

 

How can we achieve this by using script include and how can we test it by using business rule.

1 ACCEPTED SOLUTION

yad_achyut
Giga Guru

@purnendutiw  : To Achieve your requirement you can follow the given steps :

Step 1 : 
Create a script include and Save this script as a Script Include with the name DateFormatter
Step 2 :
write the given script in the created Script Include and save it

// Convert input date to GlideDateTime
        var gdt = new GlideDateTime(inputDate);

        // Extract individual components
        var day = gdt.getDayOfMonthNoTZ(); // Day of the month
        var month = gdt.getMonthUTC() + 1; // Month (0-based, so +1)
        var year = gdt.getYearUTC(); // Year

        var hours = gdt.getHourOfDayUTC(); // 24-hour format
        var minutes = gdt.getMinuteUTC(); // Minutes

        // Convert to 12-hour format with AM/PM
        var period = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12 || 12; // Convert 0-hour to 12-hour format

        // Pad day, month, and minutes with leading zeros
        day = ('0' + day).slice(-2);
        month = ('0' + month).slice(-2);
        minutes = ('0' + minutes).slice(-2);

        // Construct the formatted date
        var formattedDate = day + '/' + month + '/' + year + ' ' + hours + ':' + minutes + ' ' + period;

        return formattedDate;

Step 3: 
Create a business rule and write the following code to test it.

var formatter = new DateFormatter();
var inputDate = '2025-01-27 15:30:00'; // Example input date (in UTC format)
var formattedDate = formatter.formatDate(inputDate);
gs.print('Formatted Date: ' + formattedDate);

View solution in original post

2 REPLIES 2

JerryJ071847183
Tera Sage

For understanding the concept check the below reference link

 

LINK

 

I hope this helps.

yad_achyut
Giga Guru

@purnendutiw  : To Achieve your requirement you can follow the given steps :

Step 1 : 
Create a script include and Save this script as a Script Include with the name DateFormatter
Step 2 :
write the given script in the created Script Include and save it

// Convert input date to GlideDateTime
        var gdt = new GlideDateTime(inputDate);

        // Extract individual components
        var day = gdt.getDayOfMonthNoTZ(); // Day of the month
        var month = gdt.getMonthUTC() + 1; // Month (0-based, so +1)
        var year = gdt.getYearUTC(); // Year

        var hours = gdt.getHourOfDayUTC(); // 24-hour format
        var minutes = gdt.getMinuteUTC(); // Minutes

        // Convert to 12-hour format with AM/PM
        var period = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12 || 12; // Convert 0-hour to 12-hour format

        // Pad day, month, and minutes with leading zeros
        day = ('0' + day).slice(-2);
        month = ('0' + month).slice(-2);
        minutes = ('0' + minutes).slice(-2);

        // Construct the formatted date
        var formattedDate = day + '/' + month + '/' + year + ' ' + hours + ':' + minutes + ' ' + period;

        return formattedDate;

Step 3: 
Create a business rule and write the following code to test it.

var formatter = new DateFormatter();
var inputDate = '2025-01-27 15:30:00'; // Example input date (in UTC format)
var formattedDate = formatter.formatDate(inputDate);
gs.print('Formatted Date: ' + formattedDate);