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

Hi,

What is the business requirement for this or what are you trying to achieve. It would be helpful if you could elaborate more on the requirement so that we can come up with a different solution.

 

Thanks

Sourav

Thanks,

I have a number of different room booking forms and need to set the priority based on the meeting date.

Form 1 - If within 3 working days = P1

Form 2 - If today = P1, if within 5 working days = P2, if within 2 working days = P2

 

Hi,

You can try it with a server side code like with a business rule. In that way if you fetch the value using the getValue() method it will return in the correct format and then you can apply your logic accordingly.

 

Thanks

Sourav

@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

arobertson
Tera Guru

Thanks for your input. This requirement has now been parked and i hope to pick it back up in a month or so.