Date and Time Formatting with strftime
strftime is a JavaScript utility library that provides `strftime`-style date and time formatting, mimicking the functionality found in C's `strftime` function and Ruby's extensions. The current stable version is `0.10.3`, primarily receiving maintenance updates and bug fixes rather than active feature development. This is indicated by its infrequent release cadence since 2016's planned v1.0 release that never materialized. Key differentiators include its support for various format specifiers (including Ruby extensions), robust localization capabilities through custom locale objects or bundled identifiers, and timezone handling via offsets. It is designed to work both in Node.js environments and web browsers, offering a consistent API for formatting `Date` objects into human-readable strings. The library aims for broad compatibility, even supporting very old Node.js versions.
Common errors
-
TypeError: strftime is not a function
cause Attempting to call `strftime` as a constructor (`new strftime()`) or calling methods on an incorrectly imported `strftime` object. This can also happen if a module bundler misinterprets the CJS default export.fixEnsure `strftime` is imported correctly as a function/object (e.g., `const strftime = require('strftime');`) and called directly or its methods (`strftime(...)`, `strftime.localize(...)`). Do not use `new` with `strftime`. -
SyntaxError: Cannot use import statement outside a module or ReferenceError: require is not defined
cause These errors occur when mixing CommonJS `require()` and ES module `import` syntax in incompatible JavaScript environments, or when a CJS module is incorrectly treated as an ESM module and vice-versa.fixIn a file intended for ES Modules, ensure your `package.json` has `"type": "module"` or the file ends with `.mjs`. Use `import strftime from 'strftime';`. In a file intended for CommonJS, ensure `"type": "module"` is NOT in `package.json` or the file ends with `.cjs`. Then, use `const strftime = require('strftime');`. -
Incorrect padding for %-y (e.g., '01' instead of '1')
cause Versions of `strftime` prior to `0.10.2` contained a bug where the `%-y` specifier would incorrectly include a leading zero instead of omitting it.fixUpgrade the `strftime` package to version `0.10.2` or later to resolve the incorrect padding behavior for `%-y`.
Warnings
- breaking The `v0.10.0` release removed old API methods (such as `strftimeTZ` and `strftimeUT`) that were previously deprecated in `v0.9`.
- breaking The `v0.9.0` release introduced a significant API unification and cleanup, alongside a major performance boost. This involved changes to how the library is used, deprecating older patterns.
- gotcha Due to changes in `Date.toString()` behavior in V8 (Node.js v10+, Chrome 66+), the `%Z` specifier for time zone names may not always return the expected short name, sometimes falling back to a longer name or a generic offset. Version `0.10.1` included an attempt to work around this, but inconsistencies can still occur across different environments.
- gotcha This package is distributed exclusively as a CommonJS module. Direct ES Module `import` statements (e.g., `import strftime from 'strftime';`) may not function correctly in pure ESM Node.js environments without a bundler or specific `package.json` configuration for CJS interoperability.
Install
-
npm install strftime -
yarn add strftime -
pnpm add strftime
Imports
- strftime
import strftime from 'strftime';
const strftime = require('strftime'); - localize
const strftime = require('strftime'); const it_IT_locale = { /* ... */ }; const strftimeIT = strftime.localize(it_IT_locale); - localizeByIdentifier
const strftime = require('strftime'); const strftimeES = strftime.localizeByIdentifier('es_ES'); - timezone
const strftime = require('strftime'); const strftimeUTC = strftime.timezone(0); const strftimePDT = strftime.timezone('-0700');
Quickstart
const strftime = require('strftime');
// Get current date/time in default locale
const now = new Date();
console.log('Current time (default):', strftime('%B %d, %Y %H:%M:%S', now));
// Define a custom Italian locale
const it_IT_custom = {
identifier: 'it-IT-custom',
days: ['domenica', 'lunedi', 'martedi', 'mercoledi', 'giovedi', 'venerdi', 'sabato'],
shortDays: ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'],
months: ['gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'],
shortMonths: ['gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago', 'set', 'ott', 'nov', 'dic'],
AM: 'AM', PM: 'PM', am: 'am', pm: 'pm',
formats: {
D: '%m/%d/%y', F: '%Y-%m-%d', R: '%H:%M',
X: '%T', c: '%a %b %d %X %Y', r: '%I:%M:%S %p',
T: '%H:%M:%S', v: '%e-%b-%Y', x: '%D'
}
};
// Use a custom locale
const strftimeIT_custom = strftime.localize(it_IT_custom);
console.log('Current time (Italian custom):', strftimeIT_custom('%B %d, %Y %H:%M:%S', now));
// Use a bundled locale (e.g., French)
const strftimeFR = strftime.localizeByIdentifier('fr_FR');
console.log('Current time (French bundled):', strftimeFR('%A %d %B %Y %H:%M:%S', now));
// Format a specific date in a specific timezone (e.g., Pacific Daylight Time, -0700)
const specificDate = new Date(1678886400000); // March 15, 2023 12:00:00 UTC
const strftimePDT = strftime.timezone('-0700');
console.log('Specific date (PDT):', strftimePDT('%F %T %Z', specificDate));
// Format the same specific date in UTC
const strftimeUTC = strftime.timezone(0);
console.log('Specific date (UTC):', strftimeUTC('%F %T %Z', specificDate));