- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2023 02:14 AM
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,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 02:55 AM - edited 04-06-2023 03:01 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 03:04 AM
Try the script I gave in a background script and show the output.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 03:15 AM
Here is the output :
var gr = new GlideRecord('proc_rec_slip_item');
gr.get('e34d5f928765a5505f05ea0e8bbb35e8');
var gdt = new GlideDateTime(gr.received);
gs.print(gdt);
gdt.addYears(1);
gs.print(gdt);
gdt.addDays(1);
gs.print(gdt);
*** Script: 2022-12-19 23:00:00
*** Script: 2023-12-19 23:00:00
*** Script: 2023-12-20 23:00:00
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 03:17 AM
And this looks correct, you added a year and then a day.
What is your expected output?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 04:59 AM - edited 04-06-2023 05:00 AM
If you look at my screenshot above, the display value in the field is 20/12/2023 00:00:00
So the expected output should be 21/12/2023.
But the real value from instance timezone (i guess) is
Display value : 20/12/2023 00:00:00
Value : 19/12/2023 23:00:00
With that said, the display value in the script after adding a day will still be 20/12/2023 and not 21/12/2023.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 05:08 AM
Nevermind, it's working. i got an error in my script.
Thanks!