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

staceyrocheleau
Kilo Guru

You're setting the end_date field with just the date value but not telling it the time to use.


Can't you just do this:


var gdt = new GlideDateTime(task.start_date);


gdt.addDays(14);


task.end_date = gdt;


I'm trying to set the start_date to today and the end_date two weeks from now.   I can't prove it but it looks like ServiceNow counts today as day 1 therefore the 25th is day 14.   So I just added 15 days instead and called it done.   Argh!


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


This works better however the time is 5 hours behind me.   So I'm assuming there's a time zone setting somewhere....another search.