TypeScript
Day.js wird mit offiziellen TypeScript-Typdefinitionen im NPM-Paket ausgeliefert.
Installation via NPM
console
npm install dayjs
Import und Verwendung in Ihrer TypeScript-Datei
js
import * as dayjs from 'dayjs';
dayjs().format();
Probleme beim Import von Day.js?
Wenn Ihre tsconfig.json
die folgende Konfiguration enthält, sollten Sie den Standardimport import dayjs from 'dayjs'
verwenden:
json
{
//tsconfig.json
"compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}
}
Ohne diese Konfiguration funktioniert der Standardimport nicht korrekt und Sie müssen weiterhin import * as dayjs from 'dayjs'
verwenden.
Locale- und Plugin-Import
Um Locales (Gebietsschemas) und Plugins zu verwenden, müssen Sie zuerst das gewünschte Locale und das Plugin importieren.
js
import * as dayjs from 'dayjs';
import * as isLeapYear from 'dayjs/plugin/isLeapYear'; // Plugin importieren
import 'dayjs/locale/zh-cn'; // Locale importieren
dayjs.extend(isLeapYear); // Plugin aktivieren
dayjs.locale('zh-cn'); // Locale aktivieren