Как преобразовать секунды в формат времени чч:мм:cc ?

Moved Other
  • Господа, помогите пожалуйста, есть переменная с секундами (например 1081), как мне их преобразовать в формат времени? то есть чч:мм:cc и вывести в лог? понимаю возможно подобная тема есть на форуме, но мне не удалось найти(

  • @ElTigre said in Как преобразовать секунды в формат времени чч:мм:cc ?:

    1081

    var date = new Date(null);
    date.setSeconds(1081); // specify value for SECONDS here
    var result = date.toISOString().substr(11, 8);
    log(result);
    

    or as one line of code >

    log(new Date(1081 * 1000).toISOString().substr(11, 8))
    
  • @GaG thank you very much, it works) and another question, can you do the same thing but without a hour? only mm: ss ?)

  • First of all, the proper way to say "thank you very much, it works)"
    is to UPVOTE the post.

    Secondly, its not clear what you actually want to achieve from
    your question. But Ill give you some solutions that might work
    for you.

    If you just want to extract mm:ss from above example, you can do it
    simply by removing first 3 chars from result like this >

    log(new Date(1091 * 1000).toISOString().substr(11, 8)).substring(3);
    

    But if you need exact number of minutes and seconds from seconds
    its a different thing and sometimes it cant be output as mm:ss because
    there could be 100+ minutes in given seconds variable (3 or more
    digits amd "mm" format means max 2 digits with leading 0 for values
    smaller than 10).

    So to calculate any number of minutes and seconds from large number
    of seconds you need to do as follows >

    totalSeconds = 33649;
    minutes = Math.floor(totalSeconds / 60);
    seconds = totalSeconds % 60;
    log("minutes: " + minutes);
    log("seconds: " + seconds);
    
  • ModeratorM Moderator moved this topic from Off topic on

  • 0 Votes
    2 Posts
    485 Views
  • 0 Votes
    1 Posts
    491 Views
  • 0 Votes
    3 Posts
    636 Views
  • 0 Votes
    8 Posts
    1069 Views
  • 0 Votes
    8 Posts
    1671 Views