I'm suffering under the ambiguity of the Date object : local time vs UTC. The problem:
Users can enter the year, month, day, hour, minute that will then be used for a Date object:
const d1 = new Date(y, mo -1, d, h, m)
This creates a new Date for the user input at UTC. Whenever I call d1.toISOString() it returns the ISO string for UTC, not the local, user timezone.
Also, d1.getHours() for instance will be in local time, but with d1.getTimezoneOffset() added to the values. So
d1.getHours() != h
d1.getHours() == h - d1.getTimezoneOffset()
How can this be resolved?
I see the following documentation
Note: Where Date is called as a constructor with more than one argument, the specified arguments represent local time. If UTC is desired, use new Date(Date.UTC(...)) with the same arguments.
But I can not wrap my head around it how to properly use local and UTC times...
My question:
How shall I properly use Date to handle user input and how call the toISOString to get local timestamps? I need the string for the local date as specified by the user in ISO format. Not locale, just the numbers. I do not care about the locale output.
-1in the parameter to theDateconstructor? Additionally,new Date() === new Date(Date.now())so if you have a Unix timestamp, and callnew Datewith it it will generate a localizedDated1.toLocaleString()