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

209
Visualizações
Javascript - Setting Custom Time in Unix Timestamp

I'm working on a program that takes input from a user for a time. The program will take the information and automatically generate a Unix Timestamp using the current date as the date in the timestamp.

For example:

Daniel wants to generate a Unix Timestamp for 8:30 AM on Christmas Day. He runs a command /unix 8:30, and the console prints out 1640421000.

What's the best way to achieve this? I understand how to generate a Unix Timestamp, but how do I edit just the time to the user input. Any help would be greatly appreciated.

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

0

You can create a Date for the current date, set the time as required, then generate a time value in seconds.

If the user will always enter H:mm and you don't need to validate the input, then the following will do:

let time = '8:30';
let tv = new Date().setHours(...(time.split(/\D/)), 0) / 1000 | 0;

// Check value
console.log(new Date(tv * 1000).toString());

However, the input should be validated (e.g. hours 0-23, minutes 0-59).

about 4 years ago · Juan Pablo Isaza Relatório

0

I just went with the following:

const time = interaction.options.getString('time');
const date = new Date().toISOString().slice(0, 10);

console.log(Math.round(new Date(`${date} ${time}:00`).getTime()/1000));

Seems to work for me.

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