- 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:56 AM
Also, getDayOfWeekLocalTime returns 1 for Monday through 7 for Sunday, so you'll want to set dayofWeek = 0 if it is 7.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 12:43 PM
Thank you so much for your help, @Brad Bowman
Apologies for the delay in responding — I wanted to thoroughly test the revised code. The "Day of Week" is now working perfectly. However, I'm still encountering an issue with the time not displaying correctly. I've been reviewing the code to identify the problem but haven't been able to pinpoint the mistake yet.
Could you please continue to provide assistance with this?
User selected 12:34 PM
It is printing 08:34 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 01:26 PM
This looks like a timezone issue. Try to get the offset, then subtract or add it to the time
var offset = today.getTZOffset();
today.subtract(offset); //or today.add(offset)
var time = today.getTime();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 01:34 PM
I modified the code as suggested, but the time was supposed to be 1:30 PM, but it came out as 5:30 AM instead.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 01:42 PM
It depends on what the timezone is vs UTC. If you get 05:30 with 'add', what are the results with 'subtract', or vice-versa?