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

152
Visualizações
How to get the remaining time from 24 hours in JavaScript

I have a problem, where I try to find the number of hours and minutes after I had last collected the item. For example, if I had collected the item 1 minute ago, the code should be able to print out the remaining time left to collect the item again(23 h and 59 m) in this format. Till now, I have worked out this much:

UserJSON is a JSON file with the user's name and the time of their last claim

let deltaHour = Math.floor(new Date().getTime() - UserJSON[message.author.id].lastclaim) / (60 * 60 * 24)
let deltaRealHour = Math.floor(deltaHour)
let deltaMin = ((deltaHour % 1) / 100) * 60

Please help me out.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

getTime() is milliseconds since Jan 1, 1970, 00:00:00.000 GMT

let lastclaim = 1632196398605;

let delta = (new Date() - new Date(lastclaim)) / (60 * 1000 * 60);
let deltaHrs = Math.floor(delta);
let deltaMins = Math.floor((delta % 1) * 60);
console.log(`deltaHrs:`, deltaHrs);
console.log(`deltaMins:`, deltaMins);
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is a possible solution.

You can use the function I wrote before (timeUntilNextClaim()), supply it with the last time that your user claimed the item (in epoch seconds) and it will return to you the delta milliseconds until the item can be claimed.

You can then use the second funciton I wrote for you (toHrsAndMinutes) to turn milliseconds into hours and minutes (rounded down of course).

Please let me know if you have any questions or would like any more help with this. I'd be happy to help.

function timeUntilNextClaim(LastClaim) {
  /* this function takes an epoch time of the 
  last time the user claimed the item as an argument and 
  returns (in ms) the time until they can pick up the 
  item again */

  let now = new Date();
  // add 24 hours to last claim time to calculate next claim time
  let timeTilClaim = (LastClaim + 8.64e7) - now;
  if (timeTilClaim <= 0) {
    return 0;
  } else {
    return timeTilClaim;
  }
}

function toHrsAndMinutes(duration) {
  // this function will return an array of [hours, minutes] of a duration specified in ms
  var hours, minutes;
  hours = Math.floor(duration / 3.6e6);
  minutes = Math.floor((duration % 3.6e6) / 60000);
  return [hours, minutes];
}

function test() {
    // tests / Example Usage
    let usrLastClaimTime = new Date() - 20160000; // lets pretend they claimed it last 5.6 hours ago
    let now = new Date();
    let msTilNextClaim = timeUntilNextClaim(usrLastClaimTime);
    let hoursTilClaim, minsTilClaim;
    [hoursTilClaim, minsTilClaim] = toHrsAndMinutes(msTilNextClaim);

    console.log(`The current time is ${now}`);
    console.log(`The item was last claimed at ${Date(usrLastClaimTime)}`);
    console.log(`You can claim again in ${hoursTilClaim} Hours and ${minsTilClaim} Minutes`);
}
test();

about 4 years ago · Juan Pablo Isaza 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