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

Can you try replacing var dur = notif.u_duration.getGlideObject().getNumericValue(); line with var dur = durVal.durVal();

 

Let me know the value that you are getting in "dur" after replacing.

Can you try replacing var dur = notif.u_duration.getGlideObject().getNumericValue(); line with var dur = durVal.getNumericValue();

 

Let me know the value that you are getting in "dur" after replacing.

Omkar Mone
Mega Sage

Hi 

Please refer the below code. You can simply make use of GlideDateTime API or Date() function.

https://community.servicenow.com/community?id=community_question&sys_id=dde243a1dbd8dbc01dcaf3231f96...

 

Mark correct if it helps.

 

Regards,

Omkar Mone

www.dxsherpa.com

ARG645
Tera Guru

19800000= 330 minutes = 5 hours 30 minutes

looks like your code is adding 5 hours by default, because of timezones( Duration fields internavalue is a date afterall)

Try the below code by

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

Hi Aman,

This is working fine, but in continuation to the above script i have a doubt could you please help me out.

To the duration value (in seconds) that i am getting i need to add the current time, so i am using the below code. But it dosent seem to be working. Could you tell me what is wrong with the script.

var durVal = notif.u_duration.getDisplayValue();
var dur = notif.u_duration.getGlideObject().getNumericValue();
var durationSeconds = dur/1000;


var curTime =gs.nowDateTime();
var gdt = new GlideDateTime();
gdt.setDisplayValue(gs.nowDateTime);
gdt.addSeconds(durationSeconds);
gs.addInfoMessage(gdt.getValue());
var new_time=gdt.getValue();
gs.addInfoMessage("new time is " + new_time);