how to change duration type field value.

Star123
Tera Contributor

Hi everyone,

I am getting Business left time value in this format : 

1970-01-23 09:48:26

but I want to get Business time left values in day's and  hours format. please suggest me how to achieve this.

currently I have written this code

var gr = new GlideRecord('task_sla');
gr.addQuery('task.number', 'RMSR000010855'); // Query by task number
gr.query();

while (gr.next()) {
    gs.info('Business Time Left: ' + gr.business_time_left);
}
my requirement is on the basic Business left time I need to hide or show ours option another field.
4 REPLIES 4

J Siva
Tera Sage

Hi @Star123 

Try below scripts.

Option 1:

var gr = new GlideRecord('task_sla');
gr.addQuery('task.number', 'RMSR000010855'); // Query by task number
gr.query();

while (gr.next()) {
    gs.info('Business Time Left: ' + gr.business_time_left.getDisplayValue());
}

Option 2:

var gr = new GlideRecord('task_sla');
gr.addQuery('task.number', 'RMSR000010855'); // Query by task number
gr.query();

while (gr.next()) {
    gs.info('Business Time Left: ' + gr.business_time_left.getDurationValue());
}

Regards,
Siva

Sanjay191
Tera Sage

hello @Star123 
Please refer the below code and make changes accordingly and let me know if anything else

var start = new GlideDateTime(); // current time
var end = new GlideDateTime('1970-01-23 09:48:26');

var diff = gs.dateDiff(end,start,true);
gs.print(diff);
var totalSeconds = parseInt(diff, 10); // convert string to integer
var days = Math.floor(totalSeconds / (60 * 60 * 24));
var hours = Math.floor((totalSeconds % (60 * 60 * 24)) / (60 * 60));

gs.print(days + " days " + hours + " hours");

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

Ankur Bawiskar
Tera Patron
Tera Patron

@Star123 

this should work for you

var gr = new GlideRecord('task_sla');
gr.addQuery('task.number', 'WO0010097'); // Query by task number
gr.query();
while (gr.next()) {
    // Get the business_time_left in seconds
    var duration = new GlideDuration(gr.business_time_left.getDurationValue());

    var days = parseInt(duration.getNumericValue() / 86400000);

    var hour = duration.getByFormat('HH'); //get hour part

    var mins = duration.getByFormat('mm'); //get mins

    var finalValue = 'Days ' + days + ' Hours ' + hour;
    gs.info(finalValue);
}

Record showing the Duration:

AnkurBawiskar_1-1746785521451.png

 

Output:

AnkurBawiskar_0-1746785500374.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Star123 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader