Reputation: 597
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
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
Reputation: 241475
Moment has two different utc
functions:
One is on a moment instance, used to switch the mode to UTC.
moment(input).utc()
The other is on the moment prototype, used to interpret the input as already in UTC.
moment.utc(input)
So, you want this syntax:
moment.utc(obj.created_on).local().format()
Upvotes: 27