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

299
Visualizações
jonthornton timepicker : How to get hours and minues

I'm using the jonthornton/jquery-timepicker and could not find the hours and minutes selected.
All I could find was a string output of the form '10:30pm'.
Can the hours and minutes be accessed directly from the control?
I imagined you would be able to do this but could not find it.
The best I've been able to do is what follows, anyone got anything better?

$('#StartTime').on('change', function (timeControl) {
    var hoursString;
    if (timeControl.target.value.indexOf("am") >= 0) {
        hoursString = timeControl.target.value.replace("am", ":00 AM");
    }
    else {
        hoursString = timeControl.target.value.replace("pm", ":00 PM");
    }
    var oneDate = new Date(Date.parse("2000-01-01 " + hoursString));
    var minutes = oneDate.getMinutes();
    var hours = oneDate.getHours();

    console.log("Hours : " + hours + " | Minutes : " + minutes);
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The long-standing answer would be Moment.js but it is now considered a legacy project in maintenance mode and not recommended for new projects. There are recommendations on their website for replacements, but bringing in a new dependency for this may be overkill.

The value coming in seems to follow a format which we can rely on to make parsing easy.

Format: H:MMxx

Key:

  • H = hour, 1-2 characters
  • MM = minutes, always 2 characters
  • xx = am or pm, always 2 characters
var timeArray = timeControl.split(':');
var meridiem = timeArray[1].substring(2, 4);

var hours = parseInt(timeArray[0]) + (meridiem === 'pm' ? 12 : 0);
var minutes = parseInt(timeArray[1].substring(0, 2));
var seconds = 0;

// Local time zone, read more: https://stackoverflow.com/a/29297375/5988852
var date = new Date(); 
date.setHours(hours, minutes, seconds);

// If you want UTC instead
utc =  Date.UTC(
  date.getUTCFullYear(), 
  date.getUTCMonth(), 
  date.getUTCDate(), 
  hours, 
  minutes, 
  seconds
);
var date = new Date(utc);
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