Add 14 days to current date

stevenm
Kilo Guru

I have tried addDays and addWeeks and addDaysLocalTime but   keep getting the same incorrect answer.   I'm trying to add 14 days or 2 weeks to the current date.   Each attempt for 1-12-2017 gets me 1-25-2017 at 7:00 pm.   It doesn't even change the time.   I've seen plenty of examples on the community but they get me these results.   What am I doing wrong.  

find_real_file.png

Capturedates.PNG

1 ACCEPTED SOLUTION

Okay I misunderstood your question.



So new GlideDateTime() gets the time in the system date rather than your TZ. So it is actually giving you day 14 but in your timezone it will show as the wrong date as it's converting back to your timezone which is behind. At least that's my theory because this will work and give you what you are looking for (and will be more reliable.)



var gdt = new GlideDateTime(gs.nowDateTime());


task.start_date = gdt;


var ed = new GlideDateTime(gdt);


ed.addDays(14);


task.end_date = ed;





I ran a background script and produced the results you'd expect.


var gdt = new GlideDateTime(gs.nowDateTime());


gs.log(gdt);


var ed = new GlideDateTime(gdt);


ed.addDays(14);


gs.log(ed);



*** Script: 2017-01-12 13:04:58


*** Script: 2017-01-26 13:04:58


View solution in original post

6 REPLIES 6

gs.getDateTime() is the system timezone. If you want your timezone you can try:



var tz = gs.getSession().getTimeZone();



var gdt = new GlideDateTime(gs.nowDateTime());


gdt.setTZ(tz);


task.start_date = gdt;



var ed = new GlideDateTime(gdt);


ed.addDays(14);


task.end_date = ed;


Nikita Sangare
Tera Contributor

When you  select  Planned Start Date on  change form, validate if Planned Start Date is after 10 days of current date. If not display an error message below the field and clear the value selected.