DateDiff subtract 7 days from date
						
					
					
				
			
		
	
			
	
	
	
	
	
Options
			
				
					
	
			
		
	- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎10-10-2023 03:11 AM
Hi All,
I am working on a fix script to re-calculate metrics however I need to remove 7 days from the date but I cannot get this working it stays on the same date of 19 days. I am unsure where I am going wrong so any help would be appreciated.
var gr = new GlideRecord('metric_instance');
//gr.addEncodedQuery('definition=35edf981c0a808ae009895af7c843ace^ORdefinition=e4303c44db87245007a1253948961965');
gr.addQuery('sys_id', 'ebde03131bd5b510f102ed79b04bcbe9');
gr.query();
while (gr.next()) {
    if (gr.id.state == 5 || gr.id.state == 4) {
        gr.start = gr.id.opened_at;
        gr.end = gr.id.sys_updated_on;
		gr.end.addDaysLocalTime(-7);
        var days = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
        gr.duration = days;
        gr.update();
    }
}Cheers all!
		2 REPLIES 2
	
		
		
			
			
			
					
	
			Options
			
				
					
	
			
		
	- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎10-10-2023 03:45 AM
subtract 7 days from which date?
	Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
			
			
				
			
			
			
			
			
			
		Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Options
			
				
					
	
			
		
	- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
‎10-10-2023 04:01 AM
Hi @Kearney ,
Can you try the below code please & let me know if it works(untested).
var gr = new GlideRecord('metric_instance');
//gr.addEncodedQuery('definition=35edf981c0a808ae009895af7c843ace^ORdefinition=e4303c44db87245007a1253948961965');
gr.addQuery('sys_id', 'ebde03131bd5b510f102ed79b04bcbe9');
gr.query();
while (gr.next()) {
    if (gr.id.state == 5 || gr.id.state == 4) {
        gr.start = gr.id.opened_at;
        gr.end = gr.id.sys_updated_on;
		var startDate = new GlideDateTime(gr.start);
        var endDate = new GlideDateTime(gr.end).addDaysLocalTime(-7);
        var duration = GlideDateTime.subtract(startDate,endDate);
        gr.duration = duration.getDayPart().getGlideObject().getNumericValue()/.001;
        gr.update();
    }
}
Mark my answer helpful & accepted if it helps resolve your issue.
Thanks,
Danish
