Problem on adding 1 day to a date Time Field

Nico12
Mega Sage

Hi,

 

I need help to adding a day to a Date/Time Field.

 

The below script works fine but if the time is 00:00:00, it seems that Servicenow considering thate the field is the day before, so it does not add day visually.

 

exemple : output of the below script :

*** Script: 20/12/2022 00:00:00  => Display value in the field before adding day

*** Script: 2022-12-19 23:00:00 => when i log the field before adding day

*** Script: 2025-12-20 23:00:00 => when i log the field after adding day

 

I need to add a day considering the first line (display value) but not the second line.

var grActif = new GlideRecord('alm_hardware');
        grActif.addQuery('sys_id', 'b7b558aa8735e950277a84880cbb356d');
        grActif.query();
        while(grActif.next()){

var receiveNum = grActif.receiving_line.getDisplayValue();
var dateFinGar = 3;
        var actualDate = new GlideRecord('proc_rec_slip_item');
        actualDate.addQuery('number', receiveNum);
        actualDate.query();
        if(actualDate.next()){

            var selectedGuaranteeYears = dateFinGar;
            var receivedDate = new GlideDateTime(actualDate.received);
            receivedDate.addYearsLocalTime(selectedGuaranteeYears);
            receivedDate.addDays(1);
        }
        gs.print(receivedDate);

        }

 

 Any help would be fine.

 

Regards,

1 ACCEPTED SOLUTION

Can you show me the field where you want to add the day?

Also are you adding year to it?

 

Tried the below on a random record and it works well

 

 

var gr = new GlideRecord('change_request');
gr.get('64391d914f5c934099f1b3728110c718');

var gdt = new GlideDateTime(gr.start_date);
gs.print(gdt);
gdt.addYears(1);
gs.print(gdt);
gdt.addDays(1);
gs.print(gdt);

 

 

Output

 

*** Script: 2020-10-30 00:00:00
*** Script: 2021-10-30 00:00:00
*** Script: 2021-10-31 00:00:00

 

 

-Anurag

View solution in original post

10 REPLIES 10

Ravi50
Tera Contributor

Hello Nico,

I have same kind of requirement where adding 1 day timer to the given date.

I gave variable type as Date/Time on the form side. If I gave 03-25-2024 01:30 PM, the workflow should run at 03-26-2024 01:30 PM.

as of now I am using below script on timer activity on workflow side. The below script is resuming the timer activity at 12:00 AM 03-26-2024 which I don't want at 12:00 AM. it should run at a selected time.

var gdt = new GlideDateTime(current.date_variable);

gdt.addDaysLocalTime(1);

answer= gs.dateDiff(gs.nowDateTime(), gdt + "01:00:00", true);

Can you please help with the worked code.

Thanks!