Keshav Agrawal
Keshav Agrawal

Reputation: 597

Trying to implement momentjs to display localtime

Before asking this question I have searched all stackoverflow and read the docs but I can't just understand how to convert one UTC date to local time of an user and display it in his format. I keep trying different methods but I keep getting the same time again and again.

so my django returns obj.created_on in UTC as - 2013-12-26T13:52:24 - no timezone Info here but I know its UTC

Now I want momentjs to auto detect the user's timezone and convert it in that timezone.

Can I have proper syntax for the same?

I was trying this as well:

new_date = moment(obj.created_on).utc().local().format()

Upvotes: 11

Views: 7747

Answers (2)

Hari Prasad
Hari Prasad

Reputation: 21

moment($('#start').val()).utc().format("ddd, DD MMMM YYYY H:mm:ss");
moment($('#end').val()).utc().format("ddd, DD MMMM YYYY H:mm:ss");

will display Date format in UTC if you want the date in local format.

moment($('#start').val()).utc().local().format("ddd, DD MMMM YYYY H:mm:ss");
moment($('#end').val()).utc().local().format("ddd, DD MMMM YYYY H:mm:ss");

Default will take as GMT time. We need to convert that into our local format.

Upvotes: 2

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241475

Moment has two different utc functions:

So, you want this syntax:

moment.utc(obj.created_on).local().format()

Upvotes: 27

Related Questions