Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

708
Visualizações
Javascript - Converting total Minutes into Days, Hours and Minutes without using Math.Floor

I am having total Minutes and i want to convert that total minutes into number of Days, Hours and Minutes format.

var duration = getControlValue('incident','incident_open_duration');
var formattedDuration = duration/24/60 + ":" + duration/60%24 + ':' + duration%60);
alert(formattedDuration);

If the total minutes is 8289.66 , the above line of code returns 5.756708333333333:18.161:9.659999999999854

I don't need decimals, i want to eliminate and round off them so i used Math.floor(time/24/60) + ":" + Math.floor(time/60%24) + ':' + Math.floor(time%60) but unfortunately my tool is not supporting Math.floor function

Do we have any other way of achieving this ?

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You can use double Bitwise_NOT

var duration = 8289.66;
var cleanDuration = ~~(duration / 24 / 60) + ":" + ~~(duration / 60 % 24) + ':' + ~~(duration % 60);
console.log(cleanDuration);
//Suggested by @STh format if you need a format like HH:mm:ss
var formattedDuration = String(~~(duration / 24 / 60)).padStart(2, '0') + ":" + String(~~(duration / 60 % 24)).padStart(2, '0') + ':' + String(~~(duration % 60)).padStart(2, '0')

console.log(formattedDuration);

The snippet above adds leading zeros if the hours / minutes / seconds are only one digit via the padStart method.

Another alternative is Left_shift, Right_shift or Unsigned right shift with 0

var duration = 8289.66;
var formattedDuration = ((duration / 24 / 60) << 0) + ":" + ((duration / 60 % 24) << 0) + ':' + ((duration % 60) << 0);
console.log("Left Shift Example" , formattedDuration);

var formattedDuration = ((duration / 24 / 60) >> 0) + ":" + ((duration / 60 % 24) >> 0) + ':' + ((duration % 60) >> 0);
console.log("Rigth Shift Example" , formattedDuration);

var formattedDuration = ((duration / 24 / 60) >>> 0) + ":" + ((duration / 60 % 24) >>> 0) + ':' + ((duration % 60) >>> 0);
console.log("Unsigned right shift Example" , formattedDuration);

Or Bitwise_OR with 0

var duration = 8289.66;
var formattedDuration = ((duration / 24 / 60)|0) + ":" + ((duration / 60 % 24)|0) + ':' + ((duration % 60)|0)

console.log(formattedDuration);

about 4 years ago · Santiago Gelvez Relatório

0

There are the GET Dates method, with which you can extract the components from a date. If you only have the duration, you can, e.g., use the new Date(duration_in_ms) constructor.

So, to fit your use case, the code is:

const x = new Date(duration * 60 * 1000).getXXX()

where getXXX is one of the GET Dates-methods.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda