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

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

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 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!