Date and Time Formatting with strftime

0.10.3 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic date formatting, applying a custom locale, using a bundled locale identifier, and formatting dates with explicit timezone offsets using `strftime`.

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));

view raw JSON →