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

170
Visualizações
js storing time in js variable and pass it to an array

I'm facing a simple -yet my brain can't come up with a solution - problem involving entering/saving time entries in js. Allow me to explain:

I an input in a form to enter business hours:

 ....
  from
 <input required type="time"  id="Monday" value={monday.openhours} 
  placeholder="from"/> To
  <input required type="time" id="Monday" value={monday.closehours} 
  placeholder="to"/>
  .....

I wanted to have min and max entered for the user between 10am and 10pm so I created an array that has the following:

let defaultopenhours = "10"
let defaultclosehours = "22"
let monday = {"weekdayname":"Monday","weekdaynumber":1,"openhours":defaultopenhours,"closehours":defaultsclosehours}
let tuesday = ...same as monday so on for the rest of weekdays {wednesdy to sunday}
let schedule = []

I would then push the days into the schedule array and store it in my db. Focus on one part with me here which is the defaultopenhours var and defaultclosehours var.

When I assign 10:00 as a value and open my html to enter time, the input fields are suppose to be populated with 10:00am to 10:00pm (22:00pm) but I get this in my console:

The specified value "22" does not conform to the required format.  The format is "HH:mm", "HH:mm:ss" or "HH:mm:ss.SSS" where HH is 00-23, mm is 00-59, ss is 00-59, and SSS is 000-999.

I've never needed to work with time before in js. I'm aware of momentjs and other but I want to learn how to do it in pure js. So I'm sure there are ways to solve this problem but after readying for 2 hours and trying to enter the time values in Number(), parseInt(), parseFloat() and all kind of functions I still get that error.

Do I need to use different parts

let h = 10
let m = 00
defualt = Number(h+":"+m) or something? 

To all the law abiding citizens out there who will say that this question exists in a different corner of the internet, I hear you, I just couldn't find it. So please let others answer the question if you don't have time for me. If you do, I'm grateful for the education you share with me.

Clearly the input accepts a specific format, so how to store my values so that format is accepted by the time input type?

So my question to all the js ninjas out there, how to assign the value 10:00 or 10:00 (which is 22:00) to defaultopenhours and defaultclosehours variables?

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