getDayOfWeekLocalTime

arobertson
Tera Guru

Hi all,

I have the following issue with getDayOfWeekLocalTime. If i pick the 5th December, its returning 2 (Tuesday) instead of a 6 (Saturday). It does seems to work fine if i change the date format in my user profile to yyyy-MM-dd.

Below is my script include. I'm basically taking a datetime input, checking if its a weekday against a schedule and then trying to print the day of the week.

var UserClientUtilities = Class.create();
UserClientUtilities.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    prioritySetter: function() {

        var schedule = new GlideSchedule('7a5c6245db20e8907f8b6792ca961992'); //Catalog Item Priority Checker
        var meetingDatetime = this.getParameter('sysparm_meeting_date_time');
        var dateTime = new GlideDateTime(meetingDatetime);
        var dateOnly = meetingDatetime.split(" ")[0];
        var dayOfWeek = dateTime.getDayOfWeekLocalTime();
        var isWeekday = "No";

        if (schedule.isInSchedule(dateTime)) {
            isWeekday = "Yes";
        } else {
            isWeekday = "No";
        }

        var answer = ("Current Meeting Date: " + dateOnly + " - " + "Weekday: " + isWeekday + " - " + "Day Selected: " + dayOfWeek);

        return answer;
    },

    type: 'UserClientUtilities'
});
1 ACCEPTED SOLUTION

@arobertson 

Seems due to format

Try this

var dt = '05/12/2020 09:03:34';

var gdt = new GlideDateTime();

gdt.setDisplayValue(dt,'dd/MM/yyyy HH:mm:ss');

gs.info(gdt.getDate());

var dayOfWeek = gdt.getDayOfWeekLocalTime();

gs.info(dayOfWeek);

Output:

find_real_file.png

So update your script like this

prioritySetter: function() {

        var schedule = new GlideSchedule('7a5c6245db20e8907f8b6792ca961992'); //Catalog Item Priority Checker
        var meetingDatetime = this.getParameter('sysparm_meeting_date_time');
        var dateTime = new GlideDateTime();
        dateTime.setDisplayValue(meetingDatetime,'dd/MM/yyyy HH:mm:ss');

        var dateOnly = dateTime.getDate();
        var dayOfWeek = dateTime.getDayOfWeekLocalTime();
        var isWeekday = "No";

        if (schedule.isInSchedule(dateTime)) {
            isWeekday = "Yes";
        } else {
            isWeekday = "No";
        }

        var answer = ("Current Meeting Date: " + dateOnly + " - " + "Weekday: " + isWeekday + " - " + "Day Selected: " + dayOfWeek);

        return answer;
    },

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

11 REPLIES 11

@arobertson 

I believe your original question is answered with my comment by converting the format.

If my response helped please mark my response as correct and helpful.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@arobertson 

Hope you are doing good.

Did my reply answer your question?

If so, please mark appropriate response as correct & helpful so that the question will appear as resolved for others who may have a similar question in the future.

Thanks!
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader