Moment JS, UTC to Local time

by Abdur Rahim

HTML

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://momentjs.com/downloads/moment.js"></script>
<p id="utcTime"></p>
<p id="localTime"></p>
<p id="anotherLocalTime"></p>

JavaScript

// UTC time
let utcTime = "2017-02-02 08:00:13.567";
var utcText = moment(utcTime).format("L LT");

// First way
var offset = moment().utcOffset();
var localText = moment.utc(utcTime).utcOffset(offset).format("L LT");

// Another way
var anotherway = moment.utc(utcTime).local().format("L LT");

// Showing times
$("#utcTime").html(utcText + " (UTC Time)");
$("#localTime").html(localText + " (Local Time)");
$("#anotherLocalTime").html(anotherway + " (Local Time another way)");