【问题标题】:convert from now() using Date to Moment Timezone from America/Los_Angeles使用来自 America/Los_Angeles 的 Date 到 Moment 时区从 now() 转换
【发布时间】:2020-09-28 08:35:06
【问题描述】:

我想知道是否可以在打字稿中将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】:

这段代码的问题——

import * as moment from "moment-timezone";

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

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

import moment from "moment-timezone";

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


另见:

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 2020-03-24
    • 2019-09-01
    • 2021-02-04
    • 2019-10-24
    • 1970-01-01
    • 2012-04-22
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多