Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set angular moment timezone globally?

I used angular moment js on my angular app. I want to show date and time according to the time zone of the current user. I'm unable to apply time zone globally on my app.

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

I tried this.

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

In one of my view -

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

In one of my controller -

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

I'm not getting any changes in both cases, even after I set the time zone. Am I missing something ?

like image 539
Dino Babu Avatar asked Mar 11 '26 10:03

Dino Babu


2 Answers

You cannot change constant after you set it. As injections are not dependent on module.constant or module value, this should do the trick:

angular.module('myapp').value('angularMomentConfig', {
    timezone: 'Europe/London' // e.g. 'Europe/London'
});
like image 127
CKK Avatar answered Mar 12 '26 23:03

CKK


The timezone parameter is optional. If you omit it, it will use the time zone of the local machine automatically. That is the default behavior of moment.js and of the JavaScript Date object.

like image 42
Matt Johnson-Pint Avatar answered Mar 12 '26 22:03

Matt Johnson-Pint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!