chandler
chandler

Reputation: 1150

How to apply the local timezone offset to a utc datetime with Moment.js?

If I have a DateTime in UTC, for example: 2018-05-29T02:51:39.692104, how do I apply the current user's timezone offset to get their local time using moment.js?

Upvotes: 2

Views: 3684

Answers (1)

noder
noder

Reputation: 170

You have to use moment.local() docs

var date = "2018-05-29T02:51:39.692104";

var stillUtc = moment.utc(date).toDate(); //change utc time

var local = moment(stillUtc).local().format('YYYY-MM-DD HH:mm:ss');//change local timezone

console.log(local);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>

Upvotes: 5

Related Questions