How to calculate difference between two dates and show it on DAYS?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 12:25 PM - edited 10-11-2023 12:28 PM
Hello!
I'm working on a challenge that asks me to calculate the date difference in 2 fields of a Service Portal form and impose form blocks and error-type messages depending on the result.
The two fields in the form are of type Date (not Date/Time).
Can this be done directly via Client Script or is it better to use a combination of Script Include GlideAJAX Script + Catalog Client Script onChange?
IMPORTANT:
- The date in "data_inicio_emprestimo" CANNOT be less than the current date. If it is, the field will display an error message and the form cannot be submitted.
- The date difference between the fields (data_inicio_emprestimo / data_final_emprestimo) must be a maximum of 7 days. If the difference is greater than 7 days, the field will display an error text message and the form cannot be submitted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 12:36 PM
So far, I've built up a Script Include AND a Catalog Client Script like this.
But an error message appears in the console and I don't know why.
SCRIPT INCLUDE:
function onChange(control, oldValue, newValue, isLoading) { //Variable name = data_final_emprestimo
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var startDate = g_form.getValue('data_inicio_emprestimo');
var endDate = g_form.getValue('data_final_emprestimo');
var maxDays = 7;
var ga = new GlideAjax('global.DifferDates');
ga.addParam('sysparm_name', 'calcularDias');
ga.addParam('sysparm_start', startDate);
ga.addParam('sysparm_end', endDate);
ga.getXMLAnswer(callback);
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer > maxDays) {
g_form.showFieldMsg('data_final_emprestimo', 'Você ultrapassou o limite de 7 dias. Por favor, escolha um período mais curto', 'error');
} else {
alert('Deu errado...'); //to check if Script Include and Client Script are running ok
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 01:16 PM
Hi @reinaldo_vieira,
Its seems your script is correct instead ga.getXMLAnswer(callback); you can directly call answer no need responsexml so that line change with ga.getXML(callback);
Please mark it helpful and solution proposed if it works.
Thanks,
Anand
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 10:18 PM - edited 10-13-2023 10:32 PM
Hello, @Anand Kumar P
I tried the getXML without the "Answer" but unfortunately it didn't work. I just did a search here and I think that getXMLAnswer fits more for the challenge by taking a simple Answer from the Script Include, while the other type filters an entire XML Document.
Thanks,
Reinaldo Vieira
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 02:45 AM
var sgd1 = new GlideDate();
sgd1.setDisplayValue(this.getParameter('sysparm_start'));
var sgd2 = new GlideDate();
sgd2.setDisplayValue( this.getParameter('sysparm_end'));
var duration = GlideDate.subtract(sgd1, sgd2);
gs.print(duration.getDisplayValue());
Please mark useful if above code worked