Calculating the day of the week on a glide date (in a client script)

a99920
Kilo Contributor

I am allowing the user to select a date with the calendar widget and in an effort to to revert whatever date they chose to the previous Monday my plan is to us a switch that switches on the day of the week they choose. I am able to successfully bring in the date selected:

var selected_date = g_form.getValue('week_starts_on');

but I am unable to get this converted to a day of the week. I have tried to use the following (unsuccessfully):

var selected_day=selected_date.getDay();

as I know var selected_day=new Date().getDay(); works fine.

I'm willing to consider other alternatives but this seems to be an option that will work if i can just turn that user selected date into the day of week that it corresponds to.

1 ACCEPTED SOLUTION

HI Joe,



Default Date() function available from javascript can provide you the day of the week based on date selected by the user. You wont require to make GlideAjax calls for this sure. An example of it which is relevant for you is below. You can refer to my earlier post as well for this



var day


var selected_date = g_form.getValue('week_starts_on'); // get the value entered by user


var dObj = new Date(selected_date); // initialize the object dObj for Date() method and pass the user selected date to it


var selected_day = dObj.getDay(); // return 0 for sunday, 1 for monday and store it in selected_day variable.



I have just tested it on javascript executor of ServiceNow (press control+shift+j to launch it on the form you are working on) and it worked perfect for me



find_real_file.png




View solution in original post

10 REPLIES 10

Deepak Ingale1
Mega Sage

Hi Joe,



Could you please re-frame your question with enough background of what you are trying to achieve ?


Also please provide an info what type of field "week starts on" is?


Is it a Date or DateTime field?



1) If "week starts on" is the OOB field then you can store its value using below syntax


                                                            var userSelection = g_form.getValue("week_starts_on");



    Else if it is custom created field, then syntax would be,


                                                            var userSelection = g_form.getValue("u_week_starts_on");   // u_ is prefix for all the custom fields as well as tables



2) I assume it is DateTime field then below code will give you day of the week for user selected date


                                                            var dateUsr = new Date(userSelection.split(" ")[0]);


                                                            alert("This is " + dateUsr.getDay() + " of the week");



If it is Date field then,


                                                              var dateUsr = new Date(userSelection);


                                                              alert("This is " + dateUsr.getDay() + " of the week");


a99920
Kilo Contributor

I appologize if my last sentence was not clear. I am not sure how I can put that differently but I will try.



I am needing to convert a glide date to a particular day of the week.



So if the user were to (hypothetically) selected yesterday (1/17/2016) I want convert that to tell me that particular date is 'Sunday'.



Does that help?


For that you use "getDayOfWeek()"



You can read more about it here: GlideDateTime - ServiceNow Wiki



I doesnt return "monday" but it return 1 for monday, 2 for tuesday etc. Is this enough?



Let me know if you need any more help.



//Göran


a99920
Kilo Contributor

That is my frustration. That also should work but I end up with an error. I will go ahead and paste my error along with my client script. I was trying to avoid adding additional confusion.



onChange script error: TypeError: selected_date.getDayOfWeek is not a function function onChange_time_card_week_starts_on_3(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue == '') { return; } var day var selected_date = g_form.getValue('week_starts_on'); var selected_day = selected_date.getDayOfWeek(); switch (selected_day) { case 0: day = "Sunday"; var cdt = g_form.getValue('week_starts_on'); var addtime = 0; var addtype = 'day'; break; case 1: day = "Monday"; var cdt = g_form.getValue('week_starts_on'); var addtime = -1; var addtype = 'day'; break; case 2: day = "Tuesday"; var cdt = g_form.getValue('week_starts_on'); var addtime = -2; var addtype = 'day'; break; case 3: day = "Wednesday"; var cdt = g_form.getValue('week_starts_on'); var addtime = -3; var addtype = 'day'; break; case 4: day = "Thursday"; var cdt = g_form.getValue('week_starts_on'); var addtime = -4; var addtype = 'day'; break; case 5: day = "Friday"; var cdt = g_form.getValue('week_starts_on'); var addtime = -5; var addtype = 'day'; break; case 6: day = "Saturday"; var cdt = g_form.getValue('week_starts_on'); var addtime = -6; var addtype = 'day'; break; } var ajax = new GlideAjax('ClientDateTimeUtils'); ajax.addParam('sysparm_name', 'addDateAmount'); ajax.addParam('sysparm_fdt', cdt); ajax.addParam('sysparm_addtime', addtime); ajax.addParam('sysparm_addtype', addtype); ajax.getXML(doSomething); function doSomething(response){ var answer = response.responseXML.documentElement.getAttribute("answer"); alert(answer); } }




function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') {


          return;


    }


var day


var selected_date = g_form.getValue('week_starts_on');


var selected_day = selected_date.getDayOfWeek();



switch (selected_day) {


        case 0:


                day = "Sunday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = 0; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.


                break;


        case 1:


                day = "Monday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -1; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.


                break;


        case 2:


                day = "Tuesday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -2; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.


                break;


        case 3:


                day = "Wednesday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -3; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.


                break;


        case 4:


                day = "Thursday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -4; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.


                break;


        case 5:


                day = "Friday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -5; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.


                break;


        case 6:


                day = "Saturday";


                var cdt = g_form.getValue('week_starts_on'); //Choose the field to add time from  


                var addtime = -6; //The amount of time to add  


                var addtype = 'day'; //The time type.   Can be day, week, month, year.


                break;


}



var ajax = new GlideAjax('ClientDateTimeUtils');  


ajax.addParam('sysparm_name', 'addDateAmount');  


ajax.addParam('sysparm_fdt', cdt);  


ajax.addParam('sysparm_addtime', addtime);  


ajax.addParam('sysparm_addtype', addtype);  


ajax.getXML(doSomething);  


 


function doSomething(response){        


      var answer = response.responseXML.documentElement.getAttribute("answer");  


      alert(answer);  


}


}