TypeScript
Day.js 在 NPM 包中附带了 TypeScript 的官方类型声明,开箱即用。
¥Day.js ships with official type declarations for TypeScript in NPM package out of the box.
通过 NPM 安装
¥Install via NPM
npm install dayjs
在 TypeScript 文件中导入并使用
¥Import and use in your TypeScript file
import * as dayjs from 'dayjs'
dayjs().format()
导入 Day.js 时遇到问题?
¥Have trouble importing Day.js?
如果你的 tsconfig.json
包含以下配置,则必须执行默认导入工作流程 import dayjs from 'dayjs'
:
¥If your tsconfig.json
contains the following config, you must do the default import workflow import dayjs from 'dayjs'
:
{ //tsconfig.json
"compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
}
}
如果你没有上面的这些配置,默认导入将不起作用,你将继续使用 import * as dayjs from 'dayjs'
¥If you don't have these config above, the default import won't work, and you'll continue to have to use import * as dayjs from 'dayjs'
区域设置和插件导入
¥Locale and plugin import
要使用语言环境和插件,你首先需要导入目标语言和插件。
¥To use locale and plugin, you first need to import the targeting language and plugin.
import * as dayjs from 'dayjs'
import * as isLeapYear from 'dayjs/plugin/isLeapYear' // import plugin
import 'dayjs/locale/zh-cn' // import locale
dayjs.extend(isLeapYear) // use plugin
dayjs.locale('zh-cn') // use locale