Trying to Calculate Days remaining
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 09:47 AM
All,
I'm trying to create a client script or business rule that calculates the difference of a Warranty Expiration Date and the current date to determine a "Number of Days remaining" on the warranty.
I found this script in community to modify, but I'm still missing something.
function onChange(control, oldValue, newValue, isLoading) {
var strt = g_form.getValue(warranty_expiration); //set this as current warranty date
var end = new Date(now.getFullYear(), now.getMonth(), no.getDate()); //This retrieves the current date
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',strt);
ajax.addParam('sysparm_end',end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('u_days_of_remaining_warranty', answer);
}
Below is an error that is received.
Any help is appreciated.
Jason
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 11:15 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 11:47 AM
Found the issue. Your field is date only and our script expects date time so do below
var gdt1 = new GlideDateTime(current.warranty_expiration.getDisplayValue() + " 07:00:00");
var gdt2 = new GlideDateTime();
current.u_days_of_remaining_warranty = gs.dateDiff(gdt1, gdt2, false);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 11:48 AM
make sure you use GlideDateTime instead of GlideDate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 12:00 PM
Still not working. What is the logic behind the +"07:00:00"?
Jason.
var gdt = new GlideDateTime(current.warranty_expiration.getDisplayValue() + "07:00:00");
var gdt2 = new GlideDateTime();
current.u_days_of_remaining_warranty = gs.dateDiff(gdt, gdt2, false);
Am I missing anything here?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2018 12:12 PM
logic behind was if your field is date only than we can add time to it.
but lets just do this and see we get correct value
var gdt = new GlideDateTime(current.warranty_expiration.getDisplayValue());
var gdt2 = new GlideDateTime();
current.u_days_of_remaining_warranty = gs.dateDiff(gdt, gdt2, false);
current.update();