- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 10:42 AM
Hello,
I'm facing a challenging issue with an email script. It is printing the wrong date—November 13, 2024, which falls on a Wednesday, is being displayed as Tuesday instead. I could really use your help with this.
Please take a look at the script and let me know where I screwed it up. Thank you
(function runMailScript(current, template, email, email_action, event) {
var ritmGlide = new GlideRecord('sc_req_item');
ritmGlide.addQuery('sys_id', current.document_id.toString());
ritmGlide.query();
if (ritmGlide.next()) {
var date = ritmGlide.variables.select_start_date_and_time.getDisplayValue();
if (new GlideDateTime(date).isValid()) {
var today = new GlideDateTime(date);
var day = today.getDayOfMonthUTC();
var dayFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + day + '</span>';
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var yearFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + year + '</span>';
var dayofWeek = today.getDayOfWeekLocalTime(); // Get the correct day of the week (0 = Sunday, 6 = Saturday)
var time = today.getTime();
var timeCreated = time.getByFormat('hh:mm:ss a');
var Timesplit = timeCreated.split(":");
var strTime = Timesplit[0] + ":" + Timesplit[1] + ":" + Timesplit[2];
var spacePos = strTime.indexOf(" ");
var firstPart = strTime.substring(0, spacePos - 3);
var firstPartFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + firstPart + '</span>';
var lastPart = strTime.substring(spacePos);
var lastPartFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + lastPart + '</span>';
strTime = firstPart + lastPart;
// Get the name of the month
var monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var monthName = monthList[month - 1];
var monthNameFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + monthName + '</span>';
// Define the weekdays correctly (0 = Sunday, 6 = Saturday)
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dayName = weekday[dayofWeek]; // Correctly map the weekday
var dayNameFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + dayName + '</span>';
var startDate = 'Start Date: ';
var startDateFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;"><strong>' + startDate + '</strong></span>';
var strAt = ' at ';
var strAtFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;"><strong>' + strAt + '</strong></span>';
// Print Start Date with correct day, month, year, and time
var requiredDate = startDateFormatted + dayNameFormatted + ', ' + monthNameFormatted + ' ' + dayFormatted + '. ' + yearFormatted + strAtFormatted + strTime;
template.print(requiredDate);
} else {
template.print('<span style="color:#2f5597">' + 'Start: Not Valid Start Date');
}
}
})(current, template, email, email_action, event);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 11:07 AM
You should also change these lines
var date = ritmGlide.variables.select_start_date_and_time;
var dayofWeek = today.getDayOfWeekLocalTime(); // Get the correct day of the week (1 = Monday, 7 = Sunday)
if (dayofWeek == 7){
dayofWeek = 0;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 05:42 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 10:49 AM
I wouldn't get the display value of the field from the GlideRecord, and for cases where it will matter, use getDayOfMonthLocalTime instead of UTC since you are using local everywhere else.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 10:50 AM
It looks like you are using local time for everything except day. I think you need to update this line of code var day = today.getDayOfMonthUTC() to var day = today.getDayOfMonthLocalTime().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 11:03 AM
Hi @Brian Lancaster @Brad Bowman
I updated the code as suggested, but I'm still getting Tuesday for Nov. 13. I might have updated in the wrong area. Could you please help take a look? Thank you
(function runMailScript(current, template, email, email_action, event) {
var ritmGlide = new GlideRecord('sc_req_item');
ritmGlide.addQuery('sys_id', current.document_id.toString());
ritmGlide.query();
if (ritmGlide.next()) {
var date = ritmGlide.variables.select_start_date_and_time.getDisplayValue();
// Extract substring starting from character 11 to the end (ensure date format)
// var dateSubstring = date.substring(10); // Start from index 10 to the end of the string
if (new GlideDateTime(date).isValid()) {
var today = new GlideDateTime(date);
// var day = today.getDayOfMonthUTC();
var day = today.getDayOfMonthLocalTime();
var dayFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + day + '</span>';
var month = today.getMonthLocalTime();
var year = today.getYearLocalTime();
var yearFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + year + '</span>';
var dayofWeek = today.getDayOfWeekLocalTime(); // Get the correct day of the week (0 = Sunday, 6 = Saturday)
var time = today.getTime();
var timeCreated = time.getByFormat('hh:mm:ss a');
var Timesplit = timeCreated.split(":");
var strTime = Timesplit[0] + ":" + Timesplit[1] + ":" + Timesplit[2];
var spacePos = strTime.indexOf(" ");
var firstPart = strTime.substring(0, spacePos - 3);
var firstPartFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + firstPart + '</span>';
var lastPart = strTime.substring(spacePos);
var lastPartFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + lastPart + '</span>';
strTime = firstPart + lastPart;
// Get the name of the month
var monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var monthName = monthList[month - 1];
var monthNameFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + monthName + '</span>';
// Define the weekdays correctly (0 = Sunday, 6 = Saturday)
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dayName = weekday[dayofWeek]; // Correctly map the weekday
var dayNameFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;">' + dayName + '</span>';
var startDate = 'Start Date: ';
var startDateFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;"><strong>' + startDate + '</strong></span>';
var strAt = ' at ';
var strAtFormatted = '<span style="font-size: 12.0pt; line-height: 107%; font-family: Arial, sans-serif;"><strong>' + strAt + '</strong></span>';
// Print Start Date with correct day, month, year, and time
var requiredDate = startDateFormatted + dayNameFormatted + ', ' + monthNameFormatted + ' ' + dayFormatted + '. ' + yearFormatted + strAtFormatted + strTime;
template.print(requiredDate);
} else {
template.print('<span style="color:#2f5597">' + 'Start: Not Valid Start Date');
}
}
})(current, template, email, email_action, event);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 11:07 AM
You should also change these lines
var date = ritmGlide.variables.select_start_date_and_time;
var dayofWeek = today.getDayOfWeekLocalTime(); // Get the correct day of the week (1 = Monday, 7 = Sunday)
if (dayofWeek == 7){
dayofWeek = 0;
}