Regarding Date Calculation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 04:19 AM
How to calculate the (expected end date - 1 calendar days) in service now and provide the code for it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 04:52 AM
Hi @Apoorva A H ,
In ServiceNow, you can calculate the expected end date minus one calendar day using GlideDateTime API. Here's the code to achieve that:
// Get the current date and time
var now = new GlideDateTime();
// Add one day to the current date
var expectedEndDate = new GlideDateTime();
expectedEndDate.addDays(1);
// Subtract one calendar day from the expected end date
var oneDayBeforeExpectedEndDate = new GlideDateTime(expectedEndDate);
oneDayBeforeExpectedEndDate.subtract(1);
// Print the result
gs.info('Expected end date minus one calendar day: ' + oneDayBeforeExpectedEndDate.getDisplayValue());
This script first gets the current date and time using new GlideDateTime()
. Then, it adds one day to the current date using the addDays()
method. After that, it creates a copy of the expected end date and subtracts one calendar day from it using the subtract()
method. Finally, it prints the result using getDisplayValue()
.
You can modify this code according to your specific requirements and where you want to use it in your ServiceNow instance.
Thanks,
Ratnakar