Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JavaScript Date in local time zone

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?

like image 242
Rob Fletcher Avatar asked Nov 29 '13 10:11

Rob Fletcher


People also ask

How do I convert DateTime to another time zone?

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.

What timezone does JavaScript Date use?

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.

How do I convert one time zone to another in JavaScript?

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.

How does JavaScript handle different time zones?

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.


1 Answers

// dateString is the value from datetime-local
var dateInLocal = moment(dateString, "YYYY-MM-DDThh:mm");
like image 51
Naresha Avatar answered Oct 17 '22 02:10

Naresha