How to set angular moment timezone globally?

11,394

Solution 1

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'
});

Solution 2

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

amMoment.changeTimezone('Europe/London');

Solution 3

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.

Solution 4

I too had the same problem.

This is how to solve this out.

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

Documentation here

Share:
11,394
Dino Babu
Author by

Dino Babu

Full-stack web developer with 9+ years of hands-on experience in the software industry with knowledge of complete product lifecycle through analysis, design, frontend & backend development, testing, documentation, and delivery of web projects.

Updated on June 24, 2022

Comments

  • Dino Babu
    Dino Babu almost 2 years

    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 ?

  • abarisone
    abarisone almost 8 years
    Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.