Add 2 hours to Start date and End date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 03:32 AM
Hi all,
From a change request, I am printing Start date and End date in scripts background.
I want to add 2 hours to the Start date and End date. Using below code I can add it to either start date or End date.
But how can I add it to both Start Date and End date at once using one GlideDateTime method?
var sd = current.start_time.getDisplayValue(); //getting start date value of CR
var ed = current.end_time.getDisplayValue(); //getting end date value of CR
var gdt = new GlideDateTime(sd);
var hours = 60*60*2;
gdt.addSeconds(hours);
So, please help
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 03:36 AM
Hi @Lucky1,
Try this updated scripts,
var sd = current.start_time.getDisplayValue(); //getting start date value of CR
var ed = current.end_time.getDisplayValue(); //getting end date value of CR
var gdt = new GlideDateTime(sd);
var hours = 60*60*2;
gdt.addSeconds(hours);
current.start_time = gdt;
var gdt1 = new GlideDateTime(ed);
var hours = 60*60*2;
gdt1.addSeconds(hours);
current.end_time = gdt1;
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 03:52 AM
Hi @Lucky1,
Try this
var current = new GlideRecord("change_request");
current.get("b0dbda5347c12200e0ef563dbb9a718f");
var start_date = new GlideDateTime(current.start_date);
var end_date = new GlideDateTime(current.end_date);
var hours = 2;
/************ Before adding 2 hours ***********/
gs.print('*********** Before adding 2 hours **********');
gs.print("start_date : "+start_date);
gs.print("end_date : "+end_date);
/*********** After adding 2 hours *****************/
start_date.addSeconds(hours*60*60);
end_date.addSeconds(hours*60*60);
gs.print('*********** after adding 2 hours **********');
gs.print("start_date : "+start_date);
gs.print("end_date : "+end_date);
Output:
If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 04:15 AM
you need to do this separately as both the information are different
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 04:38 AM
Oh, Ok Ankur.
Thank you