Add 3 months to date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2015 06:16 AM
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();
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2015 01:50 AM
Try this code instead. I have done few modifications-
- DELSetEligibilityOnAssignment();
- function DELSetEligibilityOnAssignment() {
- var gr = new GlideRecord('sys_user');
- gr.addQuery('sys_id', current.assigned_to);
- gr.query();
- if (gr.next()) {
- var gdt = new GlideDateTime(gr.u_eligibility_date.toString());
- gdt.addMonthsLocalTime(3);
- gs.log("After adding 3 months eligibility date is " + gdt.toString());
- 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 " + gr.u_eligibility_date.toString());
- gr.setDisplayValue('u_eligibility_date',gdt.toString());
- gs.log("After " + gr.u_eligibility_date.toString());
- gr.u_last_upgrade_date = gs.now();
- gr.update();
- }
- }
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2015 01:52 AM
**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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2015 02:09 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2015 02:15 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2015 02:39 AM
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.