How to get today date in SANDBOX using client script? I tried the below things but

DINESH CHINNAD1
Giga Expert

Hi,

 

I am new to Servicenow


var today=new Date(); //by using these gives output as UTC form date and time, so i am unable to macth this date with my input whatever i gave in the form

 

var a= now();

var b=nowDateTime();

var right_now=g_form_Date_Time;

 

and also i tried Glide System, Ajax and some other things . Glide System(gs) and Current does not supported by SANDBOX...

 

and tried few more things but i am not able to find???

 

My question is i want to accept the Date of Birth of a person with past and present date only (current date) it will not accept the future date.....

 

Can anyone Guide me....

 

Thanks in Advance...

10 REPLIES 10


I am using Client Script in rightnow i am using SANDBOX.


SANDBOX does not support Gilde Syatem and Current and Ajax and many things





Just now i had used this to get Current date... But the below code gives "Null" as output


var ajax = new GlideAjax('MyDateTimeAjax');


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


          ajax.getXMLWait();


          var time = ajax.getAnswer();


      alert("The time is "+time);


To help with the Ajax we'd have to see the script include code you are calling.


I was able to get it in demo007 using the method here: http://wiki.servicenow.com/index.php?title=Set_Current_DateTime_In_Field


1. MAke sure your script include has client callable true.


2. add this code to script include function:


  var result = this.newItem("result");  


  result.setAttribute("date", gs.nowDateTime());



3. Client side, as you previously written:


          var ajax = new GlideAjax('MyDateTimeAjax');


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


          ajax.getXMLWait();


          ajax.getXml(ajaxResponse);



function ajaxResponse(serverResponse) {


      var result = serverResponse.responseXML.getElementsByTagName("result");


      var date_value = result[0].getAttribute("date");


  alert("The time is "+ date_value);



}


Jay_Ford
Kilo Guru

On change client script on a catalog item would look something like this if you had a 'date_of_brith' Date field on your form. You could also use a Ajax call to a script include to do the same thing.



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


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


  return;


  }



  var birthDate = g_form.getValue('date_of_birth') + ' 23:59:59';


//if you use a date/time field then just remove the trailing   + ' 23:59:59'


  alert(birthDate);


  var bdMs = getDateFromFormat(birthDate, g_user_date_time_format);


  alert(bdMs);


  var todayMs = Date.now();


  alert(todayMs);


  if(bdMs > todayMs){


  alert('You must select a date in the past');


  }




}