I wrote this email script in servicenow and working fine, by my requirement is to show…
- User
I wrote this email script in servicenow and working fine, by my requirement is to show date like 20-JAN-2024. var gdt = new GlideDateTime(); gdt.addDays(10); var futureDate - gdt.getLocalDate(Įtostring(); template.print(futureDate);
- Assistant
It looks like you want to format a date in ServiceNow to display in the format DD-MMM-YYYY (e.g., 20-JAN-2024). You have a small typo and need to use the correct date formatting functions available in ServiceNow.
Here is the corrected script:
var gdt = new GlideDateTime(); gdt.addDays(10); var futureDate = gdt.getLocalDate(); // Convert the date to the desired format var formattedDate = futureDate.getDayOfMonthLocalTime() + '-' + futureDate.getMonthNameShortLocalTime().toUpperCase() + '-' + futureDate.getYearLocalTime(); // Print the formatted date template.print(formattedDate);Explanation:
- Use
gdt.addDays(10)to add 10 days to the current date. - Use
gdt.getLocalDate()to get the Local Date part of the GlideDateTime. - Construct the desired date string format using the day, short month name, and year from the GlideDate object.
getDayOfMonthLocalTime()gets the day of the month.getMonthNameShortLocalTime().toUpperCase()gets the short name for the month and converts it to uppercase.
- Use
- User
It shows like 27- undefined - 2024
- Assistant
It seems like the method names might not be entirely correct for the GlideDate object in ServiceNow. Let's revise and make sure we're using the appropriate methods to get the required data.
Here is the revised script:
var gdt = new GlideDateTime(); gdt.addDays(10); var futureDate = new GlideDate(gdt.getLocalDate().toString()); // Manually extract and format the components of the date string var day = futureDate.getDayOfMonth(); // Get the day of the month var month = futureDate.getMonth(); // Get the month (1-12) var year = futureDate.getYear(); // Get the year // Create an array with month abbreviations var monthNames = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]; // Get the correct month abbreviation var monthString = monthNames[month - 1]; // Arrays are 0-indexed, so subtract 1 from the month // Construct the formatted date string var formattedDate = day + '-' + monthString + '-' + year; // Print the formatted date template.print(formattedDate);