Answers for "calculate years hours minutes and seconds js"

2

how to convert seconds into days hours seconds js

function secondsToDhms(seconds) {
seconds = Number(seconds);
var d = Math.floor(seconds / (3600*24));
var h = Math.floor(seconds % (3600*24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);

var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : "";
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return dDisplay + hDisplay + mDisplay + sDisplay;
}
Posted by: Guest on December-15-2020
0

calculate years hours minutes and seconds js

function formatDuration (seconds) {
  if ( seconds === 0 ) return 'now' ;
  var typeArr = ['year','day','hour','minute','second'] ;
  var sec = 0 ;
  var minute = 0 ;
  var hour = 0 ;
  var day = 0 ;
  var year = 0 ;
  minute = Math.floor(seconds / 60) ;
  sec = seconds % 60 ;
  
  hour = Math.floor(minute / 60) ;
  minute = minute % 60 ;
  
  day = Math.floor(hour / 24) ;
  hour = hour % 24 ;
  
  year = Math.floor(day / 365) ;
  day = day % 365 ;
  
  var resultArr = [year,day,hour,minute,sec] ;
  var resultStrArr = [] ;
  for ( var i = 0 ; i < resultArr.length ; i ++ ){
    if ( resultArr[i] === 1 ){
      resultStrArr.push(resultArr[i]+' ' + typeArr[i]) ;
    } else if ( resultArr[i] >= 2 ){
      resultStrArr.push(resultArr[i]+' ' + typeArr[i]+'s') ;
    }
  }
  var resultStr = '' ;
  for ( var i = 0 ; i < resultStrArr.length ; i ++ ){
    if ( i === resultStrArr.length - 2 ){
      resultStr += resultStrArr[i] + ' and ' + resultStrArr[i+1] ;
      break ;
    }
    if ( i !== resultStrArr.length -1 ){
      resultStr += resultStrArr[i] + ', ' ;
    } else {
      resultStr += resultStrArr[i];
    }
  }
  return resultStr ;
}
Posted by: Guest on October-15-2021

Code answers related to "calculate years hours minutes and seconds js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language