Timezone offset not working in angular js date expression

Issue

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.

Solution

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.)

Answered By – Ankur Choraywal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published