Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda