I have a JavaScript function that takes a number (1-31), creates a Date
and sends it to a server via an AJAX
request:
sendDate(d) {
let date = new Date(this.year, this.month, d);
htmx.ajax("GET", "/some/url", { values: { "date": date.toISOString() } });
}
The problem is, that JavaScript is doing some timezone correction which has the effect, that if I create a date like new Date(2022, 09, 14)
I get a date Wed Sep 14 2022 00:00:00 GMT+0200 (Central European Summer Time)
which when converted to ISO format becomes 2022-09-13T22:00:00.000Z
, ie. the previous day.
I know I can use .toLocaleDateString()
, but I would like to stick to the ISO-format and I would also like to avoid hacky solutions such as always creating the date with some specified time at the middle of the day or whatever.
Is there a simple way to create a regular date object and pass it on to a server without timezone shenanigans?