adopted-ember-addons / ember-moment

MIT License
400 stars 122 forks source link

Can't work out how to change timezone of a date for display #287

Closed mickbyrne closed 5 years ago

mickbyrne commented 5 years ago

I have a model that creates a property as a moment instance, always at UTC time. When being displayed in my template, I'd like to have the moment changed to a different timezone. I've tried this in my .hbs template:

{{moment-format model.timestampUTC 'd MMM, HH:mm' timeZone='Australia/Sydney'}}

But it continues to output the time in UTC. I've also tried creating a computed property on my model like:

timestampLocal: computed('timestampUTC', function() {
    return this.timestampUTC.tz('Australia/Sydney');
})

However, this throws an error saying that there's no 'tz' function on that object.

Perhaps I'm not including the moment-timezone.js thing properly? I've tried adding this to my config/environment.js file, but no luck:

module.exports = function(environment) {

let ENV = {

    // etc. etc. 

    moment: {
        includeTimezone: 'all'
    }
};

    return  ENV;
}

I feel like I'm missing something obvious...

scottkidder commented 5 years ago

I think you need to interact with the moment service to change the timezone for moment-format. Try setting the display timezone there and see if that gets you anywhere.

mickbyrne commented 5 years ago

Turns out I hadn't correctly installed the 'moment-timezone' package. Once I installed this into my Ember solution (using bower) everything started working fine.

Thanks for the help @scottkidder