moment / moment-timezone

Timezone support for moment.js
momentjs.com/timezone
MIT License
3.82k stars 837 forks source link

How to set default timezone in typescript #842

Closed aslanovsergey closed 2 years ago

aslanovsergey commented 4 years ago

Hello,

I know that moment.tz.setDefault('UTC') can be used to set default timezone, but in typescript i don't see tz property

svltmccc commented 4 years ago

Did you install @types/moment-timezone?

moment doesn't contain moment-timezone out of box like types of moment don't contain @types/moment-timezone.

aslanovsergey commented 4 years ago

Installed both, still Property 'tz' does not exist on type 'typeof moment'. Should I set it another way?

svltmccc commented 4 years ago

Can you publish your code here? In my current project I have

And I can access to tz property like:

import * as moment from "moment-timezone";

moment.tz.setDefault("UTC");

moment-timezone.d.ts just overrides moment module and appends several properties:

import moment = require('moment');

// require("moment-timezone") === require("moment")
export = moment;

declare module 'moment' {
  interface MomentZone {
    name: string;
    abbrs: string[];
    untils: number[];
    offsets: number[];
    population: number;

    abbr(timestamp: number): string;
    offset(timestamp: number): number;
    utcOffset(timestamp: number): number;
    parse(timestamp: number): number;
  }

  interface MomentTimezone {
    (): moment.Moment;
    (timezone: string): moment.Moment;
    (date: number, timezone: string): moment.Moment;
    (date: number[], timezone: string): moment.Moment;
    (date: string, timezone: string): moment.Moment;
    (date: string, format: moment.MomentFormatSpecification, timezone: string): moment.Moment;
    (date: string, format: moment.MomentFormatSpecification, strict: boolean, timezone: string): moment.Moment;
    (date: string, format: moment.MomentFormatSpecification, language: string, timezone: string): moment.Moment;
    (date: string, format: moment.MomentFormatSpecification, language: string, strict: boolean, timezone: string): moment.Moment;
    (date: Date, timezone: string): moment.Moment;
    (date: moment.Moment, timezone: string): moment.Moment;
    (date: any, timezone: string): moment.Moment;

    zone(timezone: string): MomentZone | null;

    add(packedZoneString: string): void;
    add(packedZoneString: string[]): void;

    link(packedLinkString: string): void;
    link(packedLinkString: string[]): void;

    load(data: { version: string; links: string[]; zones: string[] }): void;

    names(): string[];
    guess(ignoreCache?: boolean): string;

    setDefault(timezone?: string): MomentTimezone;
  }

  interface Moment {
    tz(): string | undefined;
    tz(timezone: string, keepLocalTime?: boolean): moment.Moment;
    zoneAbbr(): string;
    zoneName(): string;
  }

  const tz: MomentTimezone;
}
aslanovsergey commented 4 years ago

image

image

aslanovsergey commented 4 years ago

I will try import * as moment from 'moment-timezone';

svltmccc commented 4 years ago

I will try import * as moment from 'moment-timezone';

import moment from 'moment-timezone';

and

import * as moment from 'moment-timezone';

are the same if you enable allowSyntheticDefaultImports in tsconfig.json and it's not the cause of the error.

Can you share your tsconfig.json? Don't you override --types in compilerOptions?

ichernev commented 2 years ago

setDefault is part of current TS bindings, so it should work