- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2025 02:22 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2025 11:19 PM
@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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2025 10:06 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2025 11:19 PM
@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);