Date/Time manipulation for scoped app
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 05:08 PM
Hi Everyone,
Need your help on my requirement. I have a date/time field which is populated by the user. What I want is to add 1 day to the input and make sure that the hours, minutes and seconds will fall on 12mn. Example 9/15/2022 15:23:00 when save it should change to 9/16/2022 00:00:00
Not sure how to do it, hope you can help me.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 06:33 PM
Hi, exactly how you do this will depend on use of the result, IE if you need local or system time, but the syntax is basically the same using scoped GlideDateTime()
GlideDateTime | ServiceNow Developers
Something like this should work (using demo data from a PDI)
var current = new GlideRecord('incident');
current.get('1c741bd70b2322007518478d83673af3');
var myDateTime = new GlideDateTime(current.resolved_at);
gs.info('myDateTime origin ' + myDateTime);
myDateTime.addDays(1);
gs.info('myDateTime new ' +myDateTime);
var newDateTime = myDateTime.getDate() + ' 00:00:00';
gs.info('newDateTime ' + newDateTime);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 06:38 PM
Another way to skin the cat!
// Opened_at: 2022-08-29 05.23.54 AM
var gr = new GlideRecord('incident');
gr.get();
gs.info(gr.opened_at); // 2022-08-29 09:23:54
var gdt = new GlideDateTime(gr.opened_at);
gdt.addDaysUTC(1);
gs.info(gdt.getDate()); // 2022-08-30
var gdt2 = new GlideDateTime(gdt.getDate());
gs.info(gdt2); // 2022-08-30 00:00:00