Home > Article > Web Front-end > How to Parse a Date String Without Timezone Conversion in JavaScript?
When parsing a date string without a specified timezone in JavaScript, the browser interprets it based on the local timezone. This can lead to unexpected results when working with dates across different timezones. To overcome this issue, a solution is required that avoids timezone interpretation and produces a Date object corresponding to the local time.
One approach involves leveraging the getTimezoneOffset() method. This method returns the difference between the local timezone and UTC in minutes. By adding the value returned by getTimezoneOffset() multiplied by 60000 (milliseconds per minute) to the date's getTime() value, the timezone offset is corrected. Here's an example:
<code class="javascript">var date = new Date('2016-08-25T00:00:00'); var userTimezoneOffset = date.getTimezoneOffset() * 60000; var correctedDate = new Date(date.getTime() + userTimezoneOffset);</code>
This produces a Date object corresponding to the local time at the time of parsing, without any timezone conversion. It's important to note that getTimezoneOffset() can return both negative and positive values depending on the location.
The above is the detailed content of How to Parse a Date String Without Timezone Conversion in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!