To add 4 years to todays year and subtract 1 month present month
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2022 11:45 PM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2022 01:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2022 01:37 AM
how can I separate year and month and store in two different variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2022 01:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2022 11:09 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2022 02:05 AM
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);