Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!