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

Which version of SN you are using, Are you using Geneva? Fuji?



It worked fine for me in Geneva.



find_real_file.png


find_real_file.png


find_real_file.png


Hi There,

How you fixed this issue please?

Thank you!!

Hi Alex,



If it's a date field then you might need to do a little trick like that:



var gdDate = new GlideDate(),


gdFormatted;



gdDate.setDisplayValue(gr.u_eligibility_date);


gdFormatted = gdDate.getByFormat("dd-MM-yyyy");



var gdtDate = new GlideDateTime(gdFormatted);


gdtDate.addMonths(3);



Just double check if you need to do gr.u_eligibility_date.toString() just in case.




Cheers



Greg


HV1
Mega Guru

Starting with Eureka addMonths() method was replaced by the addMonthsLocalTime(int) and addMonthsUTC(int) methods.


addMonths(int)Adds a specified number of months to the current GlideDateTime object. This method was replaced by theaddMonthsLocalTime(int) and addMonthsUTC(int) methods starting with the Eureka release.
addMonthsLocalTime(int)Adds a specified number of months to the current GlideDateTime object, expressed in the user's time zone. This method is available starting with the Eureka release.
addMonthsUTC(int)Adds a specified number of months to the current GlideDateTime object, expressed in the UTC time zone. This method is available starting with the Eureka release.


On line 9 in your code, instead of converting the field value to GlideObject, try just passing the display value.



  1. var gdt = new GlideDateTime(gr.u_eligibility_date.getDisplayValue());     // The parameter here should just be date value.
  2.   gdt.addMonthsUTC(3);  


Regards,


Hardik Vora


Hi Hardik,



Are you sure it works? Would be cool as it saves some lines comparing to what I do but I get an error trying to create an object: GlideDateTime: Invalid date time: '19-Nov-2015', ignored.




Cheers



Greg