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

517
Views
HTML5: input type duration

Is there a way to use input type duration. I am trying to make a input in a way, a user to add time duration something like this 06:30:27:15 ( hh:mm:ss:ms ) and should allow only (0-23:0-59:0-59:0-59).

any help appreciated!

NOTE!: I want to implement this in Angular2+.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Sadly, neither HTML inputs nor JavaScript itself support a duration data type. <input type="time"> is for a time of the day, and may display AM/PM selectors depending on your locale.

Your best bet is to work with a text input and to use JavaScript events to automatically insert : and ignore invalid inputs. Using the pattern attribute may also help. Be sure to convert the value to a number before using it from the JavaScript side of things. Here's something to get you started.

{
  let durationIn = document.getElementById("duration-input");
  let resultP = document.getElementById("output");
  
  durationIn.addEventListener("change", function (e) {
    resultP.textContent = "";
    durationIn.checkValidity();
  });
  
  durationIn.addEventListener("invalid", function (e) {
    resultP.textContent = "Invalid input";
  });
}
input:invalid:not(:focus) { background: red; }
<input id="duration-input" type="text" required pattern="[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2}" value="00:00:00:00" title="Write a duration in the format hh:mm:ss:ms">

<p id="output"></p>

I'm sure there are many open-source libraries which provide a ready-made widget for that.

over 4 years ago · Santiago Trujillo Report

0

Maybe try something like this:

<input type="time" step="0.001">
over 4 years ago · Santiago Trujillo Report

0

<label for="appt">Choose a time for your meeting:</label>

<input type="time" id="appt" name="appt"
       min="09:00" max="18:00" required>

<small>Office hours are 9am to 6pm</small>

Try this... I hope this helps ! This is something I did for one of my projets...

over 4 years ago · Santiago Trujillo 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!