To add 4 years to todays year and subtract 1 month present month

Saikeerthana
Tera Contributor

I want update

valid_year = todays year + 4

valid_month=todays month-1

I wrote the following code, but it is not working

var gd = new GlideDateTime(gs.nowDateTime());
//gdt=gd.getYearLocalTime();
gr.u_valid_year = gd.addYearsLocalTime(4);
gr.u_valid_month = gd.addMonthsLocalTime(1);

Thanks in advance!

9 REPLIES 9

Hitoshi Ozawa
Giga Sage
Giga Sage

Following code will add 4 years and substract 1 month to the current date.

var gdt = new GlideDateTime();
gdt.addYearsLocalTime(4);
gdt.addMonthsLocalTime(-1);
gs.print(gdt);

Execution result:

*** Script: 2026-01-20 09:18:00

how can I separate year and month and store in two different variable?

Hi,

Please try below code-

 

var gdt = new GlideDateTime();
gdt.addYearsLocalTime(4);
gdt.addMonthsLocalTime(-1);

gs.print("Date: "+gdt.getDate());
gs.print("Time: "+gdt.getTime().getDisplayValue());

 

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Regards,
Saurabh


Thanks and Regards,

Saurabh Gupta

Hi,

 

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Regards,
Saurabh


Thanks and Regards,

Saurabh Gupta

Following code will set variables month and year to a date that's 4 years from now and 1 month back.

var gdt = new GlideDateTime();
gdt.addYearsLocalTime(4);
gdt.addMonthsLocalTime(-1);
var month = gdt.getMonthLocalTime();
var year = gdt.getYearLocalTime();

gs.info("year:" + year + " month:" + month);

Execution result:

*** Script: year:2026 month:1

If prefer to have date in UTC instead of local time zone, use the following code:

var gdt = new GlideDateTime();
gdt.addYearsLocalTime(4);
gdt.addMonthsLocalTime(-1);
var month = gdt.getMonthUTC();
var year = gdt.getYearUTC();

gs.info("year:" + year + " month:" + month);