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

306
Visualizações
JavaScript Timer date not Valid when using correct format

I have a string that is put together by cookies inputted by the user in a form. this is the code:

var monthCookie     = getCookieValue("monthUserPref")
var dayCookie       = getCookieValue("dayUserPref")
var hourCookie      = getCookieValue("hourUserPref")
var minuteCookie    = getCookieValue("minuteUserPref")
var ampmCookie      = getCookieValue("ampmUserPref")
var currentYear     = new Date().getFullYear();
var monthCookie     = getMonth(monthCookie);

var currentEndDateForClockNoTZChange = "'"+currentYear+", "+monthCookie+", "+dayCookie+", "+hourCookie+", "+minuteCookie+", 0'"

var endTimeNoOffset = new Date(currentEndDateForClockNoTZChange)
var endTimeOffset   = endTimeNoOffset.getTimezoneOffset();

alert("Put together time is "+currentEndDateForClockNoTZChange)
alert(endTimeNoOffset)
alert("month cookie reads '"+monthCookie+"', day cookie reads '"+dayCookie+"', hour cookie reads '"+hourCookie+"', minute cookie reads '"+minuteCookie+"', ampm cookie reads '"+ampmCookie+"'.");
alert("timer offset is "+endTimeOffset)

The first alert reads "Put together time is '2022, 1, 11, 1, 11, 0' ",
the second alert says Invalid Date",
the third alert says, month cookie reads '1',
day cookie reads '11',
hour cookie reads '1',
minute cookie reads '11',
ampm cookie reads 'PM'.",
the fourth alert says "Timer offset is NaN".

Do I have to use a different format or something?

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

0

this way:

the way you want to use the Date() constructor is about integers arguments, not a string

let
  monthCookie     = Number(getCookieValue('monthUserPref'))
, dayCookie       = Number(getCookieValue('dayUserPref'))
, hourCookie      = Number(getCookieValue('hourUserPref'))
, minuteCookie    = Number(getCookieValue('minuteUserPref'))
, ampmCookie      = Number(getCookieValue('ampmUserPref'))
, currentYear     = new Date().getFullYear()
, monthCookie     = getMonth(monthCookie)
, endTimeNoOffset = new Date( currentYear, monthCookie, dayCookie, hourCookie, minuteCookie, 0 )
, endTimeOffset   = endTimeNoOffset.getTimezoneOffset()
  ;
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