How to subtract Due Date with Current Date in Servicnow By using scheduled jobs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2024 10:31 AM
Hi Team,
We are facing Issue Management Module. I have to subtract Due Date with Current Date in Servicenow by using Scheduled jobs in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2024 11:40 AM
Hi @Arun91
Hope you have understood your requirement thoroughly and you know when to schedule run-
Here's the working script you requested-
function calculateDateDifference() {
var currentDate = new GlideDateTime();
var gr = new GlideRecord('your_table_name');
gr.addNotNullQuery('due_date_field'); // Replace with the actual field name of Due Date
gr.query();
while (gr.next()) {
var dueDate = new GlideDateTime(gr.getValue('due_date_field'));
var difference = gs.dateDiff(currentDate, dueDate, true);
gs.info('Difference for record ' + gr.getDisplayValue() + ': ' + difference + ' milliseconds');
}
}
calculateDateDifference();
Please mark my answer helpful and correct.
Regards,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-01-2024 01:35 PM - edited ‎04-01-2024 01:37 PM
Hi @Arun91 ,
In the below script i have calculated the difference between todays date and due date from Ritm, please modify it according to your need,
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=90527e0393ecc6504487bd6cebba1078^stateNOT IN3,4,7'); // change the query
gr.query();
while(gr.next()) {
var startDate = new GlideDate(); // getting the current date
var endDate = gr.due_date; // assigning the value of due date to end date variable
var gdt = new GlideDateTime(endDate); //as it was in date time getting the value of end date
gs.info('Line number 6 ' + gdt);
var dateDiff = GlideDateTime.subtract(startDate, gdt); //getting the difference between current date and due date
var roundedDate = dateDiff.getRoundedDayPart(); //TRy either ways such as getRoundedDayPart(); or getDayPart();
var dayDiff = dateDiff.getDayPart();
gs.info('Rounded value' + roundedDate );
gs.info('Difference ' + dayDiff);
}
getRoundedDayPart()
Returns the rounded number of days. If the time part is more than 12 hours, the return value is rounded up. Otherwise, it is rounded down.
getDayPart()
Returns the number of days.
Please mark this comment as Correct Answer/Helpful if it helped you
Regards,
Swathi Sarang