Animations

API

Arrays

Async

Basics

Challenges

Classes

Console

Dates

Debugging

DOM Elements

DOM Methods

DOM Navigation

DOM Properties

Event Listeners

Flow Control

Forms

Functions

Global Functions

JSON

Keywords

Libraries (3rd party)

Math

Modules

Objects

Snippets

String

Types

Widgets

Window Object

JavaScript - Converting UTC to Local

Say you have dates stored in UTC format but the users of your app are in different time zones. You probably want to display the date formatted/converted for their time zone. Here's an easy way to do that. The trick is to use the getTimezoneOffset() method off the UTC date.

 

 


let utcDate = new Date("2011-06-29 16:00:00.000");
let localDate = new Date(utcDate.getTime() - utcDate.getTimezoneOffset() * 60 * 1000);
document.getElementById("demo1").innerHTML = utcDate;
document.getElementById("demo2").innerHTML = localDate;