1function secondsToHms(d) {
2 d = Number(d);
3 var h = Math.floor(d / 3600);
4 var m = Math.floor(d % 3600 / 60);
5 var s = Math.floor(d % 3600 % 60);
6
7 var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
8 var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
9 var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
10 return hDisplay + mDisplay + sDisplay;
11}
1 var myDate = new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
2 console.log(myDate)
3//using regex
1String.prototype.toHHMMSS = function () {
2 var sec_num = parseInt(this, 10); // don't forget the second param
3 var hours = Math.floor(sec_num / 3600);
4 var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
5 var seconds = sec_num - (hours * 3600) - (minutes * 60);
6
7 if (hours < 10) {hours = "0"+hours;}
8 if (minutes < 10) {minutes = "0"+minutes;}
9 if (seconds < 10) {seconds = "0"+seconds;}
10 return hours+':'+minutes+':'+seconds;
11}
12