Regarding Date Calculation

Apoorva A H
Tera Contributor

How to calculate the (expected end date - 1 calendar days) in service now and provide the code for it?

1 REPLY 1

Ratnakar7
Mega Sage
Mega Sage

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