Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

how to check , date and time entered by user is 48hours from the current time .

vijaysirangi
Kilo Contributor

find_real_file.png

please post if any one has the script for it .

1 ACCEPTED SOLUTION

Please check your instance. I have created a new onchnage client script and script include. It is working as expected.


onChange:


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


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


  return;


  }


  var ga = new GlideAjax('ValidateDate');


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


  ga.addParam('sysparm_date',newValue);


  ga.getXML(CallBack);



  function CallBack(response) {


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


  if(answer=='true'){


  alert("pls select atleast 48hrs or more   from now");


  g_form.setValue('lpar_refreshed','');


  }


  }


}


Script include:


var ValidateDate = Class.create();


ValidateDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  dateValidation: function(){


  return (gs.dateDiff(gs.nowDateTime(),new GlideDateTime(this.getParameter('sysparm_date')), true)/3600<48);


  },


  type: 'ValidateDate'


});


View solution in original post

27 REPLIES 27

amaradiswamy
Kilo Sage

Hi Sirangi,



you can try the below



var days = gs.dateDiff(current.field_name, gs.nowDateTime(),false);



the above will give the difference in days, and you can put a condition like days >= 2



if you should do this from client-side script then create a script include and with glideajax call, you can apply the above logic.



Thanks and regards,


Swamy


Hi , thanks for response.



i will explain my requirement exactly .



when user selects some date in the calendar (as shown the varibale in the image above)   , that date or duration selected by him should be 48hrs from the time he is placing request , if   < 48hrs then a dialogue box shuld pop out saying that "Duration selected should be atleaset 48hrs minimum"/



please give the script for this .



thanks in Advance.


Hi Vijay,



I have just taken a part of the script from the below thread


Client Script Date/Time Functions




client script:


  1. var cdt = g_form.getValue('due_date'); //change the field name as per form
  2. var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.  
  3.  
  4. var ajax = new GlideAjax('ClientDateTimeUtils');  
  5. ajax.addParam('sysparm_name','getNowDateTimeDiff');  
  6. ajax.addParam('sysparm_fdt', cdt);  
  7. ajax.addParam('sysparm_difftype', dttype);  
  8. ajax.getXML(doSomething);  
  9.  
  10. function doSomething(response){  
  11. var answer = response.responseXML.documentElement.getAttribute("answer");  
  12. if(answer < 2880)
  13. {
  14. alert("Duration selected should be atleaset 48hrs minimum");
  15. g_form.setValue('field_name','');
  16. }
  17. }  

Script include


name:ClientDateTimeUtils


client callable: true



  1. var ClientDateTimeUtils = Class.create();  
  2. ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {  
  3.  
  4. //Takes a Single Date/Time Field and returns its time difference from nowDateTime().  
  5. //params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)  
  6. getNowDateTimeDiff: function(){  
  7. var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field  
  8. var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day  
  9. var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);  
  10. var timediff = this._calcDateDiff(diffTYPE, diff);  
  11. //return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;  
  12. return timediff;  
  13. },
  14. _calcDateDiff: function(diffTYPE, seconds){  
  15. var thisdiff;  
  16. if (diffTYPE == "day"){thisdiff = seconds/86400;}  
  17. else if (diffTYPE == "hour"){thisdiff = seconds/3600;}  
  18. else if (diffTYPE == "minute"){thisdiff = seconds/60;}  
  19. else if (diffTYPE == "second"){thisdiff = seconds;}  
  20. else {thisdiff = seconds;}  
  21. return thisdiff;  
  22. }  
  23. });



Thanks and regards,


Swamy


Hi , can u share your contact . i need to discuss on this issue. i tried


different ways but its not working . so if you are ok to discuss then i


will contact you .



THanks .



On Tue, Sep 13, 2016 at 10:39 AM, amaradiswamy <