How to set angular global timezone time globally?

I used angular moment js in angular app. I want to show the date and time according to the current user's timezone. I cannot apply the time zone globally.

https://github.com/urish/angular-moment

I've tried this.

angular.module('myapp').constant('angularMomentConfig', {
    timezone: 'Europe/London' // e.g. 'Europe/London'
});

      

In one of my views -

{{home.eventStart| amDateFormat:'dddd, MMMM Do YYYY, h:mm:ss a'}}

      

In one of my controllers -

$scope.profile.eventStart = moment(data.start).format('YYYY-MM-DD hh:mm a');

      

I don't get any changes in either case, even after I set the timezone. Did I miss something?

+3


source to share


5 answers


You cannot change the constant after setting it. Since injections do not depend on the value of module.constant or module, this should do the trick:



angular.module('myapp').value('angularMomentConfig', {
    timezone: 'Europe/London' // e.g. 'Europe/London'
});

      

+4


source


This parameter timezone

is optional. If you omit it, it will automatically use the local computer time zone. This is the default behavior of the default.js and the JavaScript object Date

.



+1


source


Starting with version 1.0.0-beta.1, the time zone can be set using the amMoment service . Just inject the service amMoment and call changeTimeZone:

amMoment.changeTimezone('Europe/London');

      

+1


source


I had the same problem too.

Here's how to solve it.

You need to include moment-timezone.js v0.3.0 or higher in your project, otherwise the custom timezone functions will not be available.

Documentation here

0


source


app.run(['amMoment',
    function (amMoment) {
        amMoment.changeTimezone('Europe/Paris');
    }
]);

      

-1


source







All Articles