Convert seconds to hours, minutes and seconds (JavaScript)

This is JavaScript implementation of the PHP function previously described in Convert seconds to hours, minutes and seconds (PHP). Please see the link for more detailed explanation of the function.

  1. /**
  2.  * Convert number of seconds into time object
  3.  *
  4.  * @param integer secs Number of seconds to convert
  5.  * @return object
  6.  */
  7. function secondsToTime(secs)
  8. {
  9.     var hours = Math.floor(secs / (60 * 60));
  10.    
  11.     var divisor_for_minutes = secs % (60 * 60);
  12.     var minutes = Math.floor(divisor_for_minutes / 60);
  13.  
  14.     var divisor_for_seconds = divisor_for_minutes % 60;
  15.     var seconds = Math.ceil(divisor_for_seconds);
  16.    
  17.     var obj = {
  18.         "h": hours,
  19.         "m": minutes,
  20.         "s": seconds
  21.     };
  22.     return obj;
  23. }



출처 - http://codeaid.net/javascript/convert-seconds-to-hours-minutes-and-seconds-(javascript)

Posted by linuxism
,