D3 Time Format

4.1.0 · active · verified Sun Apr 19

d3-time-format is a core D3 module providing robust JavaScript utilities for formatting and parsing dates and times, inspired by the venerable `strftime` and `strptime` functions from the C standard library. Currently stable at version 4.1.0, this package typically releases new major versions for significant breaking changes or feature additions, while minor and patch releases address bug fixes and smaller enhancements. It enables developers to convert `Date` objects into human-readable, locale-specific strings and vice-versa, making it indispensable for data visualization and applications requiring precise time representation. A key differentiator is its deep integration within the D3 ecosystem, frequently used alongside D3 time scales, and its comprehensive support for various format specifiers and internationalization through locale definitions, which were notably enhanced in recent versions like 4.1.0 with additional exports and updates. It explicitly adopted modern JavaScript features like `type: module` in v4 and ES2015 in v3, signaling a commitment to contemporary development practices.

Common errors

Warnings

Install

Imports

Quickstart

This demonstrates a multi-scale time formatter, dynamically adjusting date output based on the time interval, a common pattern in D3.

import { timeFormat } from 'd3-time-format';
import { timeSecond, timeMinute, timeHour, timeDay, timeWeek, timeMonth, timeYear } from 'd3-time';

// This quickstart demonstrates how to use d3-time-format to create a
// multi-scale time formatter, which dynamically changes the date
// format based on the time interval being displayed. This pattern is
// commonly used in D3 visualizations for axis labels or tooltips
// to provide appropriate granularity.
const formatMillisecond = timeFormat(".%L");
const formatSecond = timeFormat(":%S");
const formatMinute = timeFormat("%I:%M");
const formatHour = timeFormat("%I %p");
const formatDay = timeFormat("%a %d");
const formatWeek = timeFormat("%b %d");
const formatMonth = timeFormat("%B");
const formatYear = timeFormat("%Y");

function multiFormat(date: Date): string {
  return (timeSecond(date) < date ? formatMillisecond
      : timeMinute(date) < date ? formatSecond
      : timeHour(date) < date ? formatMinute
      : timeDay(date) < date ? formatHour
      : timeMonth(date) < date ? (timeWeek(date) < date ? formatDay : formatWeek)
      : timeYear(date) < date ? formatMonth
      : formatYear)(date);
}

// Example usage with various dates to show different formats
const now = new Date();
const dateInMs = new Date(now.getTime() - 500); // Less than a second ago
const dateInSeconds = new Date(now.getTime() - 15 * 1000); // 15 seconds ago
const dateInMinutes = new Date(now.getTime() - 5 * 60 * 1000); // 5 minutes ago
const dateInHours = new Date(now.getTime() - 3 * 60 * 60 * 1000); // 3 hours ago
const dateInDays = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000); // 2 days ago
const dateInWeeks = new Date(now.getTime() - 3 * 7 * 24 * 60 * 60 * 1000); // 3 weeks ago
const dateInMonths = new Date(now.getTime() - 2 * 30 * 24 * 60 * 60 * 1000); // 2 months ago
const dateInYears = new Date(now.getTime() - 1 * 365 * 24 * 60 * 60 * 1000); // 1 year ago

console.log(`Millisecond format: ${multiFormat(dateInMs)}`);
console.log(`Second format: ${multiFormat(dateInSeconds)}`);
console.log(`Minute format: ${multiFormat(dateInMinutes)}`);
console.log(`Hour format: ${multiFormat(dateInHours)}`);
console.log(`Day format: ${multiFormat(dateInDays)}`);
console.log(`Week format: ${multiFormat(dateInWeeks)}`);
console.log(`Month format: ${multiFormat(dateInMonths)}`);
console.log(`Year format: ${multiFormat(dateInYears)}`);

view raw JSON →