Convert second into hh:mm (hours: Minutes)

Radhe
Tera Expert

Hi All,

Please advise how to convert duration result from second to hh:mm:ss (hours: Minutes: second).

find_real_file.png

On change script-

find_real_file.png

Script include -

find_real_file.png

 

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi Radhe,

 

Replace line 8 in script include with below.

var h = Math.floor(dur / 3600);
var m = Math.floor(dur % 3600 / 60);
var s = Math.floor(dur % 3600 % 60);
var finalis=h +':'+m+':'+':'+s;

return finalis;

View solution in original post

10 REPLIES 10

Pratiksha Kalam
Kilo Sage

Hello,

Ref,HH:MM format

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

 

If answer is helpful please mark correct and helpful!
Thanks,
Pratiksha

Jyoti8
Kilo Guru

Hi Radhe,

input parameter cannot be less than Zero as you are converting the Decimal hh:mm:ss format.

decimalTohhmmss(1.5); //will give you 01:30:00
decimalTohhmmss(0.5); //will give you 00:30:00
decimalTohhmmss(-1.0); //Will give nothing

function decimalTohhmmss(decimal){
	if(decimal< 0) // return nothing if the parameter is negative
	return;
	var totalSeconds = decimal*3600;
	var hours   = Math.floor(totalSeconds / 3600);
	var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
	var seconds = totalSeconds - (hours * 3600) - (minutes * 60);
			
	// round seconds
	seconds = Math.round(seconds * 100) / 100;
			
	var result = (hours < 10 ? "0" + hours : hours);
	result += ":" + (minutes < 10 ? "0" + minutes : minutes);
	result += ":" + (seconds  < 10 ? "0" + seconds : seconds);
	return result;
};

I hope it will help you.

Please mark helpful and correct, if applicable.

Thanks..!

Could you suggest where i need to type this scripts in Onchange or  script include?

Jaspal Singh
Mega Patron
Mega Patron

Hi Radhe,

 

Replace line 8 in script include with below.

var h = Math.floor(dur / 3600);
var m = Math.floor(dur % 3600 / 60);
var s = Math.floor(dur % 3600 % 60);
var finalis=h +':'+m+':'+':'+s;

return finalis;