Home >Web Front-end >JS Tutorial >How Can I Convert UTC Date-Time to Local Browser Time in JavaScript?

How Can I Convert UTC Date-Time to Local Browser Time in JavaScript?

Barbara Streisand
Barbara StreisandOriginal
2024-12-06 12:20:13403browse

How Can I Convert UTC Date-Time to Local Browser Time in JavaScript?

Transforming UTC Date-Time to Local Browser Time

Problem:

When receiving a date-time value in UTC format from a server, such as "6/29/2011 4:52:48 PM," it becomes necessary to convert it to the user's local browser time zone using JavaScript.

Solution Using JavaScript:

To accomplish this task, append 'UTC' to the date-time string before converting it to a JavaScript Date object:

var date = new Date('6/29/2011 4:52:48 PM UTC');

This will automatically adjust the date-time to the user's local browser time, resulting in the following output when calling date.toString():

"Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"

The above is the detailed content of How Can I Convert UTC Date-Time to Local Browser Time in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn