Installation
Get started with date-and-time in your project using your preferred package manager.
Package Manager Installation
npm install date-and-timeyarn add date-and-timepnpm add date-and-timeRequirements
Runtime Requirements
- Node.js: Version 18.0.0 or higher
- Browsers: ES2021 support required
Development Requirements (optional)
- TypeScript: Version 4.5 or higher for full type support
- Modern bundler: For optimal tree-shaking (Webpack 5+, Rollup, Vite, etc.)
Import Methods
date-and-time supports both ES Modules and CommonJS, allowing you to use the import style that best fits your project.
ES Modules (Recommended)
import { format, parse } from 'date-and-time';
format(new Date(), 'YYYY/MM/DD');// => 2025/08/23CommonJS
const { format, parse } = require('date-and-time');
format(new Date(), 'YYYY/MM/DD');// => 2025/08/23Importing Locales and Timezones
Locales and timezones are distributed as separate modules to support tree shaking:
Locale Imports
// Import specific localesimport ja from 'date-and-time/locales/ja';import es from 'date-and-time/locales/es';import fr from 'date-and-time/locales/fr';
// Use in formattingformat(new Date(), 'YYYY年M月D日', { locale: ja });format(new Date(), 'D [de] MMMM [de] YYYY', { locale: es });format(new Date(), 'D MMMM YYYY', { locale: fr });For a complete list of all supported locales with import examples, see Supported Locales.
Timezone Usage
Pass an IANA timezone name string directly to any function that accepts a timezone option:
format(new Date(), 'YYYY-MM-DD HH:mm:ss', { timeZone: 'Asia/Tokyo' });format(new Date(), 'YYYY-MM-DD HH:mm:ss', { timeZone: 'America/New_York' });For a complete list of all supported timezones, see Supported Timezones.
Numeral Systems
// Import numeral systemsimport arab from 'date-and-time/numerals/arab';import beng from 'date-and-time/numerals/beng';
format(new Date(), 'DD/MM/YYYY', { numeral: arab });// => ٠٨/٠٧/٢٠٢٥Plugin Imports
Some advanced features are available as plugins:
import { format } from 'date-and-time';// Import specific pluginsimport microsecond from 'date-and-time/plugins/microsecond';import ordinal from 'date-and-time/plugins/ordinal';import zonename from 'date-and-time/plugins/zonename';
// Use plugin-specific tokens with plugins specified in optionsformat(new Date(), 'MMMM DDD, YYYY', { plugins: [ordinal] }); // with ordinal pluginformat(new Date(), 'HH:mm:ss.SSSSSS', { plugins: [microsecond] }); // with microsecond pluginCDN Usage
For browser-only projects, you can use date-and-time directly from a CDN:
Via jsDelivr
<script type="module"> import { format } from 'https://cdn.jsdelivr.net/npm/date-and-time/dist/index.js';
console.log(format(new Date(), 'YYYY/MM/DD'));</script>Via unpkg
<script type="module"> import { format } from 'https://unpkg.com/date-and-time/dist/index.js';
console.log(format(new Date(), 'YYYY/MM/DD'));</script>Bundle Size
Verification
After installation, verify that the library works correctly:
import { format } from 'date-and-time';
console.log(format(new Date(), 'YYYY/MM/DD HH:mm:ss'));// Should output current date in YYYY/MM/DD HH:mm:ss formatNext Steps
Now that you have date-and-time installed, you can:
- Quick Start - Learn the basics with simple examples
- API Reference - Dive deep into all available functions
Troubleshooting
TypeScript Issues
If you encounter TypeScript errors, ensure you’re using TypeScript 4.5+ and have the latest version of date-and-time:
npm install typescript@latest date-and-time@latestModule Resolution Issues
For Node.js projects using ES Modules, ensure your package.json includes:
{ "type": "module"}For CommonJS projects, this field should be omitted or set to "commonjs".