When creating a Date
object, it is not possible to remove timezone information due to limitations in the API.
The dates generated using different Date
APIs are interpreted based on the specified timezone (if supported by the method), or if not provided, they default to your local machine's timezone; internally, they are represented as dates relative to the UTC timezone.
When you convert a date to a string, it automatically triggers the date.toJSON()
method, which then calls date.toISOString()
, resulting in a string representation of the time relative to UTC (hence the ending Z
denoting Zulu
or UTC).
There doesn't seem to be a built-in method that serializes a date to an ISO-like string using the local timezone. If needed, you can utilize low-level Date properties to create a custom serialization method that converts back to the local timezone, or consider using a library like date-fns
. Another option is the moment
library, known for its versatility, but beware of its size and potential impact on performance.