Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

time calculator js

function TimeCalculator(seconds) {
  let y = Math.floor(seconds / 31536000);
  let mo = Math.floor((seconds % 31536000) / 2628000);
  let d = Math.floor(((seconds % 31536000) % 2628000) / 86400);
  let h = Math.floor((seconds % (3600 * 24)) / 3600);
  let m = Math.floor((seconds % 3600) / 60);
  let s = Math.floor(seconds % 60);

  let yDisplay = y > 0 ? y + (y === 1 ? " year, " : " years, ") : "";
  let moDisplay = mo > 0 ? mo + (mo === 1 ? " month, " : " months, ") : "";
  let dDisplay = d > 0 ? d + (d === 1 ? " day, " : " days, ") : "";
  let hDisplay = h > 0 ? h + (h === 1 ? " hour, " : " hours, ") : "";
  let mDisplay = m > 0 ? m + (m === 1 ? " minute " : " minutes, ") : "";
  let sDisplay = s > 0 ? s + (s === 1 ? " second" : " seconds ") : "";
  return yDisplay + moDisplay + dDisplay + hDisplay + mDisplay + sDisplay;
}
Comment

javascript calculate time

console.time("timer");   //start time with name = timer
console.timeEnd("timer"); //end timer and log time difference
Comment

PREVIOUS NEXT
Code Example
Javascript :: js shortcut 
Javascript :: add access-control-allow-origin in node js 
Javascript :: javascript parse json string 
Javascript :: pauze js 
Javascript :: regex match everything except 
Javascript :: inline if else javascript 
Javascript :: how to loop through array of numbers in javascript 
Javascript :: nativescript absolutelayout bottom 
Javascript :: react firebase hooks 
Javascript :: xmlhttprequest get request 
Javascript :: javascript onclick image 
Javascript :: javascript find string between two characters 
Javascript :: how to modify external json file javascript 
Javascript :: js for in object 
Javascript :: encodeuricomponent js 
Javascript :: Convert from JSON to Python 
Javascript :: sequelize order by 
Javascript :: nodejs current timestamp unix 
Javascript :: input onenter go to next input field javascript 
Javascript :: redirect with javascript 
Javascript :: add image in react native 
Javascript :: how to get object keys and values in javascript 
Javascript :: custom login with facebook button react native 
Javascript :: javascript array concat spread operator 
Javascript :: altv rpc 
Javascript :: get all entries in object as array hjs 
Javascript :: scroll event js 
Javascript :: javascript get boolean if checkbox is checked 
Javascript :: cryptojs decrypt 
Javascript :: uncaught TypeError: $.jajax is not a function 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =