- 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 02:22 PM
Hi @Brad Bowman
We are located in Los Angeles, California, which is in the Pacific Standard Time (PST) zone. Do you have any suggestions for modifying the code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-13-2024 05:42 PM
What is your result when you use
today.add(offset)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-14-2024 10:04 AM
Good morning @Brad Bowman
I hadn't tested today.add(offset) before, but I did now, and it's working perfectly. When you have a moment, could you please explain what the following code means?
var offset = today.getTZOffset(); today.subtract(offset); //or today.add(offset) var time = today.getTime();
We are grateful to have you in the community. Your contributions and support make a real difference. Many thanks to you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-14-2024 10:13 AM
Glad you got this working! getTZOffset will return the number of milliseconds that the local time varies from UTC, then you either have to add or subtract it from the UTC time to get the equivalent of the time when using getDisplayValue. In your case, offset will be a negative number, so by subtracting it you were adding to UTC, and adding the negative number subtracts from UTC. Clear as mud?
Connect with me https://www.linkedin.com/in/brad-bowman-321b1567/