Challenging Script Issue: Email Printing Incorrect Date and Time

Jessica28
Tera Guru

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.

 

Jessica28_0-1731523153972.png

 

 

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);

 

2 ACCEPTED SOLUTIONS

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;
}

 

View solution in original post

What is your result when you use 

today.add(offset)

View solution in original post

13 REPLIES 13

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?

What is your result when you use 

today.add(offset)

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.

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?

https://www.servicenow.com/docs/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/... 

 

 

 

Connect with me https://www.linkedin.com/in/brad-bowman-321b1567/