I'm trying to parse the value of a datetime-local
input so the format is 'yyyy-mm-ddTHH:mm'
. I want the assumption to be that I'm parsing in the local time zone of the user, i.e. if they enter 09:00
they mean 9am in their time zone.
Also not just the current time offset, e.g. if the user enters a date in June then they mean daylight savings time regardless of the fact it's now November and DST has ended.
I've tried using regular Date objects and moment.js but the assumption is always that the time zone is UTC.
Is there a way to do this?
ToLocalTime method behaves identically to the DateTime. ToLocalTime method. It takes a single parameter, which is the date and time value, to convert. You can also convert the time in any designated time zone to local time by using the static ( Shared in Visual Basic) TimeZoneInfo.
JavaScript's internal representation uses the “universal” UTC time but by the time the date/time is displayed, it has probably been localized per the timezone settings on the user's computer.
To convert a date to another time zone: Use the toLocaleString() method to get a string that represents the date according to the provided time zone. Pass the result to the Date() constructor. The returned Date object will have its date and time set according to the provided time zone.
The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.
// dateString is the value from datetime-local
var dateInLocal = moment(dateString, "YYYY-MM-DDThh:mm");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With