Typescript


2.13.0 版本开始,Moment 会包含一个 typescript 定义文件。

通过NPM安装。

npm install moment

导入并在你的 Typescript 文件中使用。

import * as moment from 'moment';

let now = moment().format('LLLL');

如果你在导入时遇到麻烦。

对于 Typescript 2.x,则尝试在 tsconfig.json 文件中的 compileOptions 中添加 "moduleResolution": "node",然后使用以下任何语法:

import * as moment from 'moment';
import moment = require('moment');

对于 Typescript 1.x,则尝试在 tsconfig.json 文件中的 compileOptions 中添加 "allowSyntheticDefaultImports": true,然后使用语法:

import moment from 'moment';

语言环境导入

若要使用 moment.locale,则首先需要导入要使用的语言。

import * as moment from 'moment';
import 'moment/locale/pt-br';

console.log(moment.locale()); // en
moment.locale('fr');
console.log(moment.locale()); // en
moment.locale('pt-BR');
console.log(moment.locale()); // pt-BR

As of version 2.13.0, Moment includes a typescript definition file.

Install via NPM

npm install moment

Import and use in your Typescript file

import * as moment from 'moment';

let now = moment().format('LLLL');

Note: If you have trouble importing moment

For Typescript 2.x try adding "moduleResolution": "node" in compilerOptions in your tsconfig.json file and then use any of the below syntax

import * as moment from 'moment';
import moment = require('moment');

For Typescript 1.x try adding "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file and then use the syntax

import moment from 'moment';

Locale Import

To use moment.locale you first need to import the language you are targeting.

import * as moment from 'moment';
import 'moment/locale/pt-br';

console.log(moment.locale()); // en
moment.locale('fr');
console.log(moment.locale()); // en
moment.locale('pt-BR');
console.log(moment.locale()); // pt-BR