How can we set end date as one month from today's date

Shantharao
Kilo Sage

How can we set end date as one month from today's date?

7 REPLIES 7

Shamma Negi
Kilo Sage
Kilo Sage

Hi Burra,



You can either write onLoad or onChange Client script and set the date after one month from the below code:



or you can also write this in Before BR:



  1. var gdt = current.u_last_test_date.getGlideObject();  
  2. if(current.business_criticality=='1 - Critical'){  
  3.   current.u_next_test_date=gdt.addDays(182); //adding 182 days to last tested date  
  4. }  


Also see this link:



Client Script Date/Time Functions



Regards,


Shamma


Regards,Shamma Negi

Hi Shamma,



We can not use "Current" object in Client scripts.



Thanks,


Buura


Hi Burra,



The example I have given that is for On Before Business Rule not Client Script.



You cannot do this directly in Client script you need to write the Script Inlcude and then use Glideajax in your Client script:



See below:



Client Script:


  1. var cdt = g_form.getValue('due_date'); //Choose the field to add time from  
  2. var addtime = 3; //The amount of time to add  
  3. var addtype = 'day'; //The time type.   Can be second, minute, hour, day, week, month, year.  
  4.  
  5. var ajax = new GlideAjax('ClientDateTimeUtils');  
  6. ajax.addParam('sysparm_name', 'addDateTimeAmount');  
  7. ajax.addParam('sysparm_fdt', cdt);  
  8. ajax.addParam('sysparm_addtime', addtime);  
  9. ajax.addParam('sysparm_addtype', addtype);  
  10. ajax.getXML(doSomething);  
  11.  
  12.  
  13. function doSomething(response){  
  14.       var answer = response.responseXML.documentElement.getAttribute("answer");  
  15.       //You could then take the new Date/Time answer and set the value of another field.  
  16.       g_form.setValue('expected_start', answer);    
  17.       alert(answer);  
  18. }


Script Include:



  1. addDateTimeAmount: function(){  
  2. var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field  
  3. var addTYPE = this.getParameter('sysparm_addtype'); //What to add - second (addSeconds()), minute (need to add conversion), hour (need to add conversion), day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())  
  4. var addTIME = this.getParameter('sysparm_addtime'); //How much time to add  
  5. var day = GlideDateTime(firstDT);  
  6.  
  7. if(addTYPE == 'second'){day.addSeconds(addTIME);}  
  8. else if (addTYPE == 'minute'){day.addSeconds(addTIME*60);}  
  9. else if (addTYPE == 'hour'){day.addSeconds(addTIME*(60*60));}  
  10. else if (addTYPE == 'day'){day.addDays(addTIME);}  
  11. else if (addTYPE == 'week'){day.addWeeks(addTIME);}  
  12. else if (addTYPE == 'month'){day.addMonths(addTIME);}  
  13. else if (addTYPE == 'year'){day.addYears(addTIME);}  
  14. else {day.addDays(addTIME);}  
  15.  
  16. //return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;  
  17. return day;  
  18. }



This would work.



Regards,


Shamma


Regards,Shamma Negi