Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to convert seaconds into hh:mm:ss in javascript

var date = new Date(0);
date.setSeconds(45); // specify value for SECONDS here
var timeString = date.toISOString().substr(11, 8);
console.log(timeString)
Comment

Convert milliseconds to hh:mm:ss javascript

function padTo2Digits(num) {
  return num.toString().padStart(2, '0');
}

function convertMsToHM(milliseconds) {
  let seconds = Math.floor(milliseconds / 1000);
  let minutes = Math.floor(seconds / 60);
  let hours = Math.floor(minutes / 60);
  seconds = seconds % 60;
  minutes = seconds >= 30 ? minutes + 1 : minutes;
  minutes = minutes % 60;
  hours = hours % 24;
  return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}`;
}

console.log(convertMsToHM(54000000)); //  15:00 (15 hours)
console.log(convertMsToHM(86400000)); //  00:00 (24 hours)
console.log(convertMsToHM(36900000)); //  10:15 (10 hours, 15 minutes)
console.log(convertMsToHM(15335000)); //  04:16 (4 hours, 15 minutes, 35 seconds)
console.log(convertMsToHM(130531000)); // 36:16 (36 hours 15 minutes 31 seconds)
Comment

PREVIOUS NEXT
Code Example
Javascript :: dropdown validation using jquery 
Javascript :: mui date picker width 
Javascript :: check if all values in array are negative javascript 
Javascript :: reactjs join two array 
Javascript :: get specific parent element jquery 
Javascript :: jquery get all input name and values and submit 
Javascript :: JS toString adds backslash 
Javascript :: react-active link 
Javascript :: angular material remove outline 
Javascript :: getfullyear javascript 
Javascript :: filter duplicates multidimensional array javascript 
Javascript :: convert array to object 
Javascript :: react native jest snapshot 
Javascript :: js add array to array 
Javascript :: disable button using jquery 
Javascript :: how to import svg in react 
Javascript :: select element by id 
Javascript :: frequency of characters in a string in javascript 
Javascript :: react context 
Javascript :: Disabale Back History On Browsers JavaScript Jquery 
Javascript :: jquery show hide based on data attribute 
Javascript :: debouncing js 
Javascript :: convert an object to an array 
Javascript :: console.log json shopify 
Javascript :: javascript insert html before element 
Javascript :: datepicker select date programmatically bootstrap 
Javascript :: onload submit form only once 
Javascript :: javascript array any 
Javascript :: useref react 
Javascript :: htpp status 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =