Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting does not work on timezone abbreviation. #128

Closed
MikeRomaa opened this issue Dec 26, 2020 · 2 comments
Closed

Formatting does not work on timezone abbreviation. #128

MikeRomaa opened this issue Dec 26, 2020 · 2 comments

Comments

@MikeRomaa
Copy link

I am trying to display a timestamp to the user in the format 16:00 EST using the following code segment:

import React from 'react'
import Moment from 'react-moment'
import 'moment-timezone'

export default class MyComponent extends React.Component {
    render() {
        return (
            const timestamp = '2021-01-03T21:00:00Z'
            <Moment local format="HH:mm z">{timestamp}</Moment>
        )
    }
}

However, instead of 16:00 EST I am just getting 16:00.

I have tried this same formatting string on regular JavaScript moment and it worked perfectly fine.

@headzoo
Copy link
Owner

headzoo commented Dec 30, 2020

I did a little bit of digging and it appears the "z" and "zz" format options are deprecated. See moment/moment#162

Base on the suggestions in that thread "z" works for me when I import "moment-timezone", and I either set Moment.globalTimezone or add the tz attribute to Moment. For example:

<Moment local format="HH:mm z" tz="America/New_York">{timestamp}</Moment>

Or

Moment.globalTimezone = 'America/New_York';

<Moment local format="HH:mm z">{timestamp}</Moment>

@MikeRomaa
Copy link
Author

Ahh I see, the inclusion of tz did fix it, thank you so much for the insight! What I ended up doing is using tz.guess() instead of just displaying all times in Eastern time 😛:

import React from 'react'
import Moment from 'react-moment'
import moment from 'moment/moment'
import 'moment-timezone'

export default class MyComponent extends React.Component {
    render() {
        return (
            const timestamp = '2021-01-03T21:00:00Z'
            <Moment local format="HH:mm z" tz={moment.tz.guess()}>{timestamp}</Moment>
        )
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants