使用Date从America / Los_Angeles将Date转换为Moment时区从now()转换

问题描述 投票:2回答:1

我想知道在打字稿中是否有可能将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'
javascript node.js typescript momentjs moment-timezone
1个回答
0
投票

此代码的问题—

import * as moment from "moment-timezone";

-是在ES6中,import * as moment语法将始终将moment导入为non-callable模块。

TypeScript过去是不兼容的,无论如何,您都可以调用这些导入。但这不会在今天的TS中实现。相反,我们使用较短的代码:

import moment from "moment-timezone";

如果使用该代码,则moment().tz("America/Los_Angeles").format()应该有效。


另请参见:

© www.soinside.com 2019 - 2024. All rights reserved.