Display users local zone abbreviation

后端 未结 5 1938
耶瑟儿~
耶瑟儿~ 2020-12-17 10:17

I was trying to use z or zz in format to display the timezone at the end of the string and found that it\'s deprecated. Z and ZZ

5条回答
  •  半阙折子戏
    2020-12-17 10:34

    As of moment-timezone 0.1.0, you can use the formatting additions to get the abbreviation of a specific time zone:

    moment().tz("America/Los_Angeles").format('z');  // PST or PDT
    

    As of version 0.5.0, you can now guess the user's local time zone, which when combined with the above technique, allows you to do the following:

    moment().tz(moment.tz.guess()).format('z');
    

    Assuming the guess was correct, it will display the associated abbreviation.

提交回复
热议问题