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-19-2015 08:26 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 10:50 PM
Hi There,
How you fixed this issue please?
Thank you!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2015 08:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2015 09:11 AM
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.
- var gdt = new GlideDateTime(gr.u_eligibility_date.getDisplayValue()); // The parameter here should just be date value.
- gdt.addMonthsUTC(3);
Regards,
Hardik Vora
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2015 09:21 AM
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