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
Release: 2024-12-06 12:20:13
Original
437 people have browsed it

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');
Copy after login

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)"
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template