从 America/Los_Angeles 使用 Date 从 now() 转换为 Moment Timezone

convert from now() using Date to Moment Timezone from America/Los_Angeles

我想知道是否可以在打字稿中将 Date() 转换为 moment().tz("America/Los_Angeles").format();

我试过了

import { MomentTimezone }  from 'moment-timezone';
const moment : MomentTimezone = new MomentTimezone(Date(), "America/Los_Angeles");

但是我有这个错误:

S2693: 'MomentTimezone' only refers to a type, but is being used as a value here

做的时候

import * as moment from "moment-timezone";
let now = moment();

我有这个错误:

TS2349: This expression is not callable.   Type 'typeof import("C:/Users/sandro/IdeaProjects/booking/node_modules/moment/moment.d.ts")' has no call signatures.

const moment: MomentTimezone = {date: Date(), timezone: 'America/Los_Angeles'};

但是我得到了

TS2322: Type '{ date: string; timezone: string; }' is not assignable to type 'MomentTimezone'.   Object literal may only specify known properties, and 'date' does not exist in type 'MomentTimezone'

这段代码的问题—

import * as moment from "moment-timezone";

——在 ES6 中,import * as moment 语法将始终导入 moment 作为 不可调用的 模块。

TypeScript 过去是不兼容的,无论如何都让你调用这些导入。但这在今天的 TS 中行不通。相反,我们使用更短的:

import moment from "moment-timezone";

如果您使用该代码,那么 moment().tz("America/Los_Angeles").format() 应该可以工作。


另请参阅: