- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 04:29 AM
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'
});
Solved! Go to Solution.
- Labels:
-
Command Line Interface
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2022 03:53 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2022 04:42 AM
You don't need to do all this overngineering.
There are quite a lot of methods in the GlideDate and GlideDateTime APIs that will help you to set the date in the desired format as well as to calculate the difference.
https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_GlideDateScopedAPI?navFilter=glidedate
Please refer to the API docs on the Developer site. There are quite a lot of script examples as well.
Mark Correct AND Helpful. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2022 03:38 AM
Ok,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2022 03:51 AM
Then you do the calculations in a client-callable script include and call the result via glide ajax.
Please refer to the following example: https://community.servicenow.com/community?id=community_blog&sys_id=dc49feeadbdd7f0c54250b55ca96191d
Mark Correct AND Helpful. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2022 03:53 AM
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.