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

Problem here is that GlideDateTime needs to be in a server script.



Alittle help of know what you need to run och server and what to run on client.



If you look at the breadcrumb for GlideDateTime it says server in it...



GlideDateTime


  Home > Script > Server APIs   > Glide Server APIs > GlideDateTime


This is why your client script don't recognize getDateOfWeek().
First line: selected_date.getDayOfWeek is not a function

do you have a suggestion on how I would go about taking this user selected date and converting it to the day of week via the client script?


GlideAjax 😃



I've just posted an example in the post how to use GlideAjax in client script and how the script include could look like. Take a look and let me know if you got any other questions.



Link: Re: Force users to add attachments


I am new to GlideAjax but am I on the right track here?



CLIENT SIDE:


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


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


          return;


    }



var ga = new GlideAjax('checkDayOfWeek');


ga.addParam('sysparm_name','checkDayOfWeek');


ga.addParam('sysparm_date',g_form.getValue('week_starts_on'));



ga.getXML(check);



function check(response){        


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


      alert(answer);


}}



SCRIPT INCLUDE:


var checkDayOfWeek = Class.create();


checkDayOfWeek.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      checkDayOfWeek: function() {


            return this.getParameter('sysparm_date').getDayOfWeek();


}});



I believe I am close but for some reason this is evaluating to NULL...If I change the return line in my script to remove .getDayOfWeek() it successfully returns the date.


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