Convert seconds to human readable time duration

后端 未结 3 1206
谎友^
谎友^ 2020-12-03 07:11

How would I best convert 90060 (seconds) to a string of \"25h 1m\"?

Currently I\'m doing this in SQL:

SELECT 
  IF(
    HOUR(
      sec_to_time(
             


        
相关标签:
3条回答
  • 2020-12-03 07:47
    TIME_FORMAT(SEC_TO_TIME(task_records.time_spent),'%Hh %im')
    

    Documentation is your friend:

    • http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

    According to comment:

    DROP FUNCTION IF EXISTS GET_HOUR_MINUTES;
    CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
      RETURNS VARCHAR(16)
    
      BEGIN
      DECLARE result VARCHAR(16);
      IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
      ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
      RETURN result;
      END
    
    DELIMETER ;
    

    Usage:

    SELECT GET_HOUR_MINUTES(task_records.time_spent) FROM table
    
    0 讨论(0)
  • 2020-12-03 07:55

    you can use predefined function sec_to_time()

    sec_to_time(number_of_seconds)

    http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time

    0 讨论(0)
  • 2020-12-03 08:03

    try this: (input your pure seconds time)

    var hours = Math.floor(input/3600);
    var minutes = Math.floor((input-hours*3600)/60);
    var seconds = input-(minutes*60)-(hours*3600);
    function convertTime(){
    return hours":"minutes":"seconds;
    }
    
    0 讨论(0)
提交回复
热议问题