Ankur Choraywal Ankur Choraywal - 1 month ago 14
AngularJS Question

Timezone offset not working in angular js date expression

I have

2016-10-21T13:47:02.922452
as ISO string from backend server.
My timezone is +0530 GMT i.e Offset is +530 (ahead of GMT).

When i use angular date expression like this

{{'2016-10-21T13:47:02.922452'| date:'medium':'+530'}}


I expected output to be = Oct 21, 2016 7:17:02 PM
but it prints
Oct 21, 2016 1:47:02 PM instead.


I am confused for what am i doing wrong here.

Answer

Answering myself ! The easiest way that i figured out ! Make a custom filter

   app.filter('IST', function($filter){
        return function(val){
            var date = new Date(val);
            return $filter(date, 'medium');
        }
    })

Then use filter in expression like this -

{{'2016-10-21T13:47:02.922452'| IST}}

Custom filter will automatically convert ISO format string to Date object (browser timezone will automatically take care of timezone conversion.)

Comments