Use date dd/mm/yyyy

Alexandre17
Tera Expert

I need calculate diference between two dates. A crazy calculate with month and dates.

But, the problem is, sometimes the Servicenow gets dd/mm/yyyy and sometimes gets mm/dd/yyyy.

Before. the code setValue in record, but this part is ok. The only problem is about get the correct value.

So, i need help to solve this.

client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var basecontrato = g_form.getValue('contrato');//ctt tb7
var basedata = g_form.getValue('data_alteracao');//data alteração tb7

var ga = new GlideAjax('x_bdb_5ea_00000147.Calc_dias_meses_tab_7_aditivos');
ga.addParam('sysparm_name', 'getDateDetails');
ga.addParam('database', basedata);
ga.addParam('contratobase', basecontrato);

ga.getXMLAnswer(getResponse);



function getResponse(response) {
var res = JSON.parse(response);

g_form.setValue('dias_sislog', res.calcdias);
var cl_a = g_form.getValue('dias_sislog');
if(cl_a=="undefined"){
g_form.clearValue('dias_sislog');
}

g_form.setValue('meses_sislog', res.calcmeses);
var cl_b = g_form.getValue('meses_sislog');
if(cl_b=="undefined"){
g_form.clearValue('meses_sislog');
}

var og_agpvalor = g_form.getValue('valor_mensal').replace(/[.]/g,'');
var agpvalor = parseFloat(og_agpvalor.replace(/[,]/g,'.'));

var og_perevt = g_form.getValue('percentual_servico_eventual'.replace(/[.]/g,''));
var perevt = parseFloat(og_perevt.replace(/[,]/g,'.'));

g_form.setValue('calculo_agrupamento', ((agpvalor/30)*parseFloat(res.calcdias)+(agpvalor*parseFloat(res.calcmeses))).toFixed(2));
g_form.setValue('calculo_agrupamento_eventual', (((agpvalor/30)*parseFloat(res.calcdias)+(agpvalor*parseFloat(res.calcmeses)))*perevt/100).toFixed(2));

}

script include

//script include
var Calc_dias_meses_tab_7_aditivos = Class.create();
Calc_dias_meses_tab_7_aditivos.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getDateDetails: function() {
var database = this.getParameter('database');
var contratobase = this.getParameter('contratobase');
obj={};

var tableContratos = new GlideRecord('x_bdb_5ea_00000147_contratos');
tableContratos.addQuery('sys_id', contratobase);
tableContratos.query();
if (tableContratos.next()) {
var datacontrato = tableContratos.getValue('datavigenciafim');
//pega o número do contrato
}

var inobjDate = new GlideDateTime(database);
var indt = inobjDate.getDate();
var inyear = indt.getYearUTC();
var inmonth = indt.getMonthUTC();
var ainday = indt.getDayOfMonthUTC();

var fimobjDate = new GlideDateTime(datacontrato);
var fimdt = fimobjDate.getDate();
var fimyear = fimdt.getYearUTC();
var fimmonth = fimdt.getMonthUTC();
var afimday = fimdt.getDayOfMonthUTC();

if (ainday >= 30){
var inday = 30;
}
else inday = ainday;

if (afimday >= 30){
var fimday = 30;
}
else fimday = afimday;

var difyear = fimyear - inyear;
var difmonth = fimmonth - inmonth;
var difday = fimday - inday;

var calc_a = ((30-inday)+fimday);
var calc_b = difday;

if (ainday == 1&&afimday==31){
obj.calcdias=0;
}

if (fimday < inday) {
obj.calcdias = calc_a;
}
else {
obj.calcdias = calc_b;
}

var dif = GlideDateTime.subtract(inobjDate,fimobjDate);
var dias = dif.getRoundedDayPart();
var calc = (dias/30);
var meses = Math.floor(calc);

difyear = fimyear - inyear;
difmonth = fimmonth - inmonth;
difday = afimday - ainday;

var diasiguais = (difyear*12)+difmonth;

if (inday==fimday){
obj.calcmeses = diasiguais;
}
else{
obj.calcmeses = meses; // return the calculated value
}

return JSON.stringify(obj);
},
type: 'Calc_dias_meses_tab_7_aditivos'
});
1 ACCEPTED SOLUTION

In most client side environments one has function getDateFromFormat and constants g_user_date_format and g_user_date_time_format to allow obtaining the date or date/time as milliseconds since 1970-01-01 00:00:00 no matter which format the current user employs;

B.t.w the statement

sometimes the Servicenow gets dd/mm/yyyy and sometimes gets mm/dd/yyyy

is not entirely accurate, it is not SN that "gets" sometimes this and sometimes that, but the user chooses to use a format that suits its needs.

Back to the issue at hand, to get the date from field data_alteracao, you could write:

getDateFromFormat(g_form.getValue('data_alteracao'), g_user_date_format);

if this field is of type Date or

getDateFromFormat(g_form.getValue('data_alteracao'), g_user_date_time_format);

if this field is of type Date/Time.

You will obtain the milliseconds I was talking about that you can than use both to set the value of a Date object or send it through GlideAjax to set the value of a GlideDate - or of a GlideDateTime object.

View solution in original post

7 REPLIES 7

So @Martin Ivanov  and @János , i cound not use client script with script include, and i change(ed) to business rule. Its works, but need "save" to do the br...

Martin Ivanov
Giga Sage
Giga Sage

Hi. If your issue is resolved and my response has helped, plese consider marking Correct and Helpful. Thanks!

Martin Ivanov

2022 Community Rising Star

 


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024