Converting Duration to seconds returning wrong value

sanvi
Tera Expert

Hi experts,

I am trying the below code to convert the duration field (u_duration) to seconds, but its not returing the correct value.

var notif = new GlideRecord('u_notifications');
notif.addQuery('u_active', true);
notif.addQuery('u_company', cmp);
notif.orderBy('u_duration');
notif.query();
while(notif.next()){

var durVal = notif.u_duration.getDisplayValue();
gs.addInfoMessage("Duration value" + durVal);
var dur = notif.u_duration.getGlideObject().getNumericValue();
var durationSeconds = (dur/1000);
gs.addInfoMessage(dur);
gs.addInfoMessage(durationSeconds);
}

 

and the duration field value is 30 min, but converting 30 min the value should be 1800 seconds, but the value that i am receiving is 19800, which is not correct. Can anyone please suggest.

find_real_file.png

1 ACCEPTED SOLUTION

Your script is getting the value in UTC Time zone because you are using getValue on a GlideDateTime Object. You need use getDisplayValue to get the Local Time.

 

var gDuration= new GlideDuration(durVal); //durVal is the Display Value of the Duration field, which you already defined in your script

var dur = gDuration.getNumericValue(); //spits out the duration value in Milliseconds in your case it should be 180000

var durationSeconds = dur/1000;
 
var curTime =gs.nowDateTime();
var gdt = new GlideDateTime();
gdt.setDisplayValue(gs.nowDateTime);
gdt.addSeconds(durationSeconds);
gs.addInfoMessage(gdt.getDisplayValue());//Loalize the time here
var new_time=gdt.getDisplayValue();//Loalize the time here
gs.addInfoMessage("new time is " + new_time);

View solution in original post

11 REPLIES 11

SanjivMeher
Kilo Patron
Kilo Patron

Can you try

 

var notif = new GlideRecord('u_notifications');
notif.addQuery('u_active', true);
notif.addQuery('u_company', cmp);
notif.orderBy('u_duration'); 
notif.query();
while(notif.next()){

var durVal = notif.u_duration.getDisplayValue();
gs.addInfoMessage("Duration value" + durVal);
var dur = notif.u_duration.getNumericValue();
var durationSeconds = (dur/1000);
gs.addInfoMessage(dur);
gs.addInfoMessage(durationSeconds);
}


Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,

I am getting NaN and undefined values for the above code. It didnt work.

SaiRaviKiran Ak
Giga Guru

You are getting the wrong value in var "dur".

Change the line var dur = notif.u_duration.getGlideObject().getNumericValue(); to get correct value.

The value should be 1800000  So that if you convert that dur value(1800000) to seconds then you will get required value as 1800(1800000/1000) .

Hi ravi,

I have used the same line code. But still the value that i am receiving is "19800". Can you suggest.