Setting a default time in a date/time field using a client script.

m_ahmedmod
Giga Contributor

Hi,

I'm trying to set a default date and time in a time/date field using a client script, it should be set to todays date and a time of 5pm. Does anyone have an idea on how this might be possible.
I thought it might look like this but it doesn't seem to work:

function onLoad() {
g_form.setValue('start_date',gs.now()+' 17:00:00');
}

Thanks in advance.

8 REPLIES 8

Sreeja Gattu
Kilo Expert

Hi,


We cannot use "gs" in client scripts.


Create a Script Include having a function which returns the required date and time and that date can be set using a client script.


adiddigi
Tera Guru

You are doing it wrong. You cannot use `gs` on Client Side. You will have to do it by writing a GlideAjax.



This is the Client Script :



function onLoad() {


      //Type appropriate comment here, and begin script below


      var ajax = new GlideAjax('MyDateTimeAjax');


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


      ajax.getXML(function () {


              g_form.setValue('date', (ajax.getAnswer()+' 17:00:00'));


      });


}




Define the Script Include MyDateTimeAjax.



Go to Script Includes -> Create new



Name : MyDateTimeAjax


Client Callable : true ( check it)



Code :



var MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      nowDateTime: function () {


              return gs.now();


      }


});



Should do the trick...


snowist
Kilo Contributor

Hi Abhiram ?? Any idea of adding 7 days and display the value to the current Date ???


Hi,



You can get that done by below code.



You can have the below script to to set the Planned End dates (for example end date after 7 days). You can have below the onLoad client script (System Definition -> Client Scripts).to set you planned end.



Client Script:


*********************************************************************


function onLoad() {


  //Type appropriate comment here, and begin script below



  var ajax = new GlideAjax('NeededDateBy');


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


  ajax.getXML(function () {


  var neededDate = ajax.getAnswer();


  g_form.setValue('u_planned_end', neededDate);


  });


}



Client Script will do GlideAjax to Script include.


Script Include: NeededDateBy (System Definition -> Script Include)


*********************************************************************


var MyNeededDateBy = Class.create();


MyNeededDateBy.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  neededByDateTime: function () {


  var neededDate = gs.nowDateTime();


  var gdt = new GlideDateTime(neededDate);


  gdt.setDisplayValue(neededDate, "yyyy-MM-dd HH:mm:ss");


  gdt.addDaysLocalTime(7);


  return gdt.getDisplayValue();


  },


  type: 'MyNeededDateBy'


});