Answers for "hours minutes seconds millisec difference 2 dates javascript"

5

javascript get hours difference between two dates

// date1 and date2 are date objects

let hours = Math.abs(date1 - date2) / 36e5;

// The subtraction returns the difference between the two dates in milliseconds.
// 36e5 is the scientific notation for 60*60*1000, dividing by which converts
// the milliseconds difference into hours.
Posted by: Guest on January-12-2021
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 "hours minutes seconds millisec difference 2 dates javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language