I wonder why the next line works:
new Date("06/19/2019 06:42 EDT") //Wed Jun 19 2019 13:42:00 GMT+0300
But this one does not:
new Date("06/19/2019 06:42 CEST") //Invalid date
I wonder why the next line works:
new Date("06/19/2019 06:42 EDT") //Wed Jun 19 2019 13:42:00 GMT+0300
But this one does not:
new Date("06/19/2019 06:42 CEST") //Invalid date
Constructors for JavaScript Date
objects can use any of these syntax options:
1) new Date();
2) new Date(year, monthIndex [, day [, hours [, minutes [, seconds [,
milliseconds]]]]]);
3) new Date(value);
4) new Date(dateString);
Your examples are using option #4, datestring
-- regarding which, n.b.:
"Parsing of date strings with the Date constructor ... is strongly discouraged due to browser differences and inconsistencies."
dateString
option for your constructor, the string needs to be "specified in a format recognized by the Date.parse() method" (that is, either an IETF-compliant RFC 2822 timestamp or a string in a version of ISO8601).
This formatting requirement is likely where your attempt is going sideways -- but as mentioned above, results will vary by browser -- so you might consider constructing a default Date object and using the setters provided by the Date
class to assign your desired values.
For example (if the timezone you want matches the timezone where your script is running):
const date = new Date();
date.setFullYear(2019);
date.setMonth(0); // Note that the months array starts with zero for January
date.setDate(1);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
console.log(date.toLocaleString()); // Logs `1/1/2019, 12:00:00 AM`
Lots more information can be found on this MDN reference page.