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-12-2023 06:13 AM
HII @reinaldo_vieira
I have worked this script based on your requirement,here is the script and code
Scriptinclude:
Mark my answer helpful & accepted if it helps you resolve your query.
Regards
Sharan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 10:49 PM
Hello, @SHARANsnow7
I tried getXML but unfortunately it didn't work. From what I read later in this article, I think getXMLAnswer was a better fit for my attempt.
I'll share the code that worked for me.
Thanks,
Reinaldo Vieira
var DifferDates = Class.create();
DifferDates.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calcularDias: function() {
var from = new GlideDate();
from.setDisplayValue(this._transformDate(this.getParameter('sysparm_start'))); //define um valor de data usando o formato de exibição e o fuso horário do usuário atual
var to = new GlideDate();
to.setDisplayValue(this._transformDate(this.getParameter('sysparm_end')));
var difference = GlideDate.subtract(from, to);
var days = difference.getDayPart(); //retorna o número de dias
return days;
},
_transformDate: function(date) {
date = date.split('/');
return date = date[2] + '-' + date[1] + '-' + date[0];
},
type: 'DifferDates'
});
function onChange(control, oldValue, newValue, isLoading) {
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) {
if (response > 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-13-2023 10:30 PM
Hello, @Jagadish Sanadi
I've tested up here with some other conversations on the subject and I like this solution. I would also add an internal function to the Include script to transform the date - in the GlideDate documentation, the expected format for the subtract is yyyy-MM-dd.
Best regards,
Reinaldo Vieira
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 11:20 PM
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');
}
}
Please check this and mark useful if this helped you