Skip to content

Installation

Get started with date-and-time in your project using your preferred package manager.

Package Manager Installation

Terminal window
npm install date-and-time

Requirements

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.

import { format, parse } from 'date-and-time';
format(new Date(), 'YYYY/MM/DD');
// => 2025/08/23

CommonJS

const { format, parse } = require('date-and-time');
format(new Date(), 'YYYY/MM/DD');
// => 2025/08/23

Importing Locales and Timezones

Locales and timezones are distributed as separate modules to support tree shaking:

Locale Imports

// Import specific locales
import 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 formatting
format(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.

CommonJS’s require() returns the same locale object as the ESM default export:

const ja = require('date-and-time/locales/ja');
format(new Date(), 'YYYY年M月D日', { locale: ja });

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 systems
import arab from 'date-and-time/numerals/arab';
import beng from 'date-and-time/numerals/beng';
format(new Date(), 'DD/MM/YYYY', { numeral: arab });
// => ٠٨/٠٧/٢٠٢٥

The same applies to numeral systems under CommonJS:

const arab = require('date-and-time/numerals/arab');
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 plugins
import { formatter as ordinal } from 'date-and-time/plugins/ordinal';
import { formatter as zonename } from 'date-and-time/plugins/zonename';
// Use plugin-specific tokens with plugins specified in options
format(new Date(), 'MMMM DDD, YYYY', { plugins: [ordinal] }); // with ordinal plugin
format(new Date(), 'YYYY-MM-DD HH:mm:ss z', { plugins: [zonename], timeZone: 'Asia/Tokyo' }); // with zonename plugin

CDN 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 format

Next Steps

Now that you have date-and-time installed, you can:

  1. Quick Start - Learn the basics with simple examples
  2. API Reference - Dive deep into all available functions

Troubleshooting

TypeScript Issues

If you encounter TypeScript errors, make sure your tsconfig.json sets moduleResolution to bundler, node16, or nodenext. The type definitions are resolved through the package’s exports field, which the legacy node (node10) setting ignores. Also make sure you have the latest versions of TypeScript and date-and-time:

Terminal window
npm install typescript@latest date-and-time@latest

Module 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".