Adding Days to a date via Script

mballinger
Mega Guru

Hello,

I have a UI Action that is used to add days/weeks to a date. It uses a prompt. A user can enter a number. That number is passed to a script include via glideajax. In the GlideAjax, I perform the calculations. This is working, but the issue I am currently facing is for some records, the date is short by 1 date. The field I have is a GlideDate field, but I am using GlideDateTime() to handle the addition.

Please see the script below:

var entries = this.getParameter('sysparm_entry_ids');
var daysOrWeeks = this.getParameter('sysparm_entry_days_weeks');
var days = this.getParameter('sysparm_days').toString(); //returns a number
var weeks = this.getParameter('sysparm_weeks').toString(); //returns a nuber
var gr = new GlideRecord('u_compliance');
gr.addQuery('sys_id', 'IN', entries);
gr.query();
while (gr.next()) {
    var currentDate = new GlideDateTime(gr.u_date);
    if (daysOrWeeks == "days") {
        currentDate.addDays(days);
    } else if (daysOrWeeks == "weeks") {
        currentDate.addWeeks(weeks);
    }

    gr.u_new_date = currentDate;
    gr.update();
}

Thanks!

1 ACCEPTED SOLUTION

Hi,

Please try using addDaysLocalTime(days) instead of addDays(days).

If that does not work then you can try using addDaysUTC(days);

 

Check below documentation to learn difference.

https://developer.servicenow.com/print_page.do?release=paris&category=null&identifier=c_APIRef&modul...

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

4 REPLIES 4

Logan Poynter
Mega Sage
Mega Sage

Hello mballinger,

By "short by 1 date" do you mean if the value is today (16th) it shows as 15th? If you just log out currentDate before adding days, is it correct?

GlideDateTime() gets the time in the system date rather than your TZ. So it's possibly bringing back a day short based on TZ differences.

 

Please mark helpful/correct if this has helped you. 

Thanks,
Logan

Thanks for your response Logan! When I say 1 day short, let’s say a user wants to add 15 days to the date field and 10/01/2022 is the date set. Using the code above , for some records, instead of the new date now being 10/16/2022, the date is getting set to 10/15/2022. The date field we are using is a date field only. What I don’t understand is, for some records it sets it properly and others it doesn’t. I believe it may be a system issue as well, but at the same time all records are using the same date type.

Hi,

Please try using addDaysLocalTime(days) instead of addDays(days).

If that does not work then you can try using addDaysUTC(days);

 

Check below documentation to learn difference.

https://developer.servicenow.com/print_page.do?release=paris&category=null&identifier=c_APIRef&modul...

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Thanks Anil!