How to use moment-timezone in Angular 2 class?

前端 未结 4 2045
梦毁少年i
梦毁少年i 2021-02-07 09:24

I am trying to use moment-timezone in my class. This is my typings.

\"moment\": \"github:DefinitelyTyped/DefinitelyTyped/moment/moment.d.ts#a1575b96ec38e91675062         


        
4条回答
  •  攒了一身酷
    2021-02-07 09:59

    I will update this for 2020.

    $ npm install moment-timezone @types/moment-timezone
    
    import moment from 'moment-timezone';
    
    ...
    
    // do this - outside of any class is fine    
    moment.tz.add('America/Los_Angeles|PST PDT|80 70|01010101010|1Lzm0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0');
    
    
    ...
    
    someMethod() {
        const m = moment(the_date);
        ...
        const mFormatted = m.tz('America/Los_Angeles').format('YYYY-MM-DDTHH:mm:ssZZ');
    }
    

    Timezone defs can be found at https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json. I use this to define my own rather than read them all in.

提交回复
热议问题