Add 3 months to date.

arobertson
Tera Guru

Hi All,

I'm struggling to add 3 months to an existing date field. The following code below should look up the user and add 3 months to their eligibility date.

DELSetEligibilityOnAssignment();

function DELSetEligibilityOnAssignment() {

  var gr = new GlideRecord('sys_user');

  gr.addQuery('sys_id', current.assigned_to);

  gr.query();

  var gdt = new GlideDateTime(gr.u_eligibility_date.getGlideObject());

  gdt.addMonths(3);

  if (gr.next()) {

  if (current.u_reason_for_assigning == 'Lost') {

  gr.u_work_notes = "Eligibility date changed on " + current.asset_tag + " for the following reason: " + current.u_reason_for_assigning + ".";

  gs.log("Before " + gdt + gr.u_eligibility_date);

  gr.u_eligibility_date = gdt;

  gs.log("After " + gdt + gr.u_eligibility_date);

  gr.u_last_upgrade_date = gs.now();

  gr.update();

  }

  }

}

24 REPLIES 24

Try this code instead. I have done few modifications-



  1. DELSetEligibilityOnAssignment();  
  2.  
  3. function DELSetEligibilityOnAssignment() {  
  4.  
  5.   var gr = new GlideRecord('sys_user');  
  6.   gr.addQuery('sys_id', current.assigned_to);  
  7.   gr.query();  
  8.    
  9.  
  10.   if (gr.next()) {  
  11.   var gdt = new GlideDateTime(gr.u_eligibility_date.toString());
  12.   gdt.addMonthsLocalTime(3);
  13.   gs.log("After adding 3 months eligibility date is " + gdt.toString());
  14.  
  15.   if (current.u_reason_for_assigning == 'Lost') {  
  16.   gr.u_work_notes = "Eligibility date changed on " + current.asset_tag + " for the following reason: " + current.u_reason_for_assigning + ".";  
  17.   gs.log("Before " + gr.u_eligibility_date.toString());  
  18.   gr.setDisplayValue('u_eligibility_date',gdt.toString());
  19.   gs.log("After " + gr.u_eligibility_date.toString());  
  20.   gr.u_last_upgrade_date = gs.now();  
  21.   gr.update();  
  22.   }  
  23.   }  
  24. }  

**EDIT: Replaced gr.setDisplayValue with gr.setValue and that seemed to fix it.**



Thanks 443923



Sorry for the delay in replying, i was on holiday last week. I just tried your suggestion and it does not work, however i do see the following in the logs.



After adding 3 months eligibility date is 2016-02-29 00:00:00



Before 2015-11-30


After 2015-11-30



So it looks like it's getting it right in the log, but then not setting the field in the user profile.


Hi Alex,



It is because using GlideDateTime methods with Date data won't work. You need to first format date in the way GlideDateTime accepts - per my last suggestion. You trying to fetch 19-Nov-2015 - which GlideDateTime doesn't understand - that is why you have odd dates like 2016-02-29 00:00:00 after addMonthsLocatTime is used.



Edit: Interesting, will have to try it out.



Cheers



Greg


Hi Alex,



I got solution for this requirement. Client Script Date/Time Functions .


function onLoad() {



var cdt = g_form.getValue('startdate'); //Choose the field to add time from  



var addtime = 36; //The amount of time to add   //you need to use months here
var addtype = 'hour'; //The time type.   Can be second, minute, hour, day, week, month, year.  
 
var ajax = new GlideAjax('ClientDateTimeUtils');  
ajax.addParam('sysparm_name', 'addDateTimeAmount');  
ajax.addParam('sysparm_fdt', cdt);  
ajax.addParam('sysparm_addtime', addtime);  
ajax.addParam('sysparm_addtype', addtype);  
ajax.getXML(doSomething);  




function doSomething(response){  
  var answer = response.responseXML.documentElement.getAttribute("answer");  
    //You could then take the new Date/Time answer and set the value of another field.  
    // g_form.setValue('expected_start', answer);  
//g_form.enddate.setDisplayValue(answer);
alert("enddate :" +answer);  
g_form.setValue('enddate', answer);
}  


 



}



Thanks & Regards


Govind Kumar Sharma


Just replace line 18 i.e.


gr.setDisplayValue('u_eligibility_date',gdt.toString());


with


gr.u_eligibility_date = gdt.toString();



This will do the job for you.



Thanks,


Tanaji Patil



--Mark correct/helpful if it help solving your issue.