I would like to know if it is possible in typescript to convert Date()
to moment().tz("America/Los_Angeles").format();
I have tried
import { MomentTimezone } from 'moment-timezone';
const moment : MomentTimezone = new MomentTimezone(Date(), "America/Los_Angeles");
but I have this error:
S2693: 'MomentTimezone' only refers to a type, but is being used as a value here
when doing
import * as moment from "moment-timezone";
let now = moment();
I have this error:
TS2349: This expression is not callable. Type 'typeof import("C:/Users/sandro/IdeaProjects/booking/node_modules/moment/moment.d.ts")' has no call signatures.
and
const moment: MomentTimezone = {date: Date(), timezone: 'America/Los_Angeles'};
but I got
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'
The problem with this code—
import * as moment from "moment-timezone";
—is that in ES6, import * as moment
syntax will always import moment
as a non-callable module.
TypeScript used to be non-compliant, and let you call these imports anyway. But that won't fly in today's TS. Instead, we use the shorter:
import moment from "moment-timezone";
If you use that code then moment().tz("America/Los_Angeles").format()
should work.
See also: