'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?



Solution 1:[1]

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()
  ;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mister Jojo