Vega Time Utilities

3.1.0 · active · verified Sun Apr 19

vega-time is a JavaScript utility package providing a suite of date and time manipulation functions specifically designed for use within the Vega visualization ecosystem. It offers functionalities for parsing, formatting, flooring, and ceiling time values, crucial for handling temporal data in data visualizations. As part of the larger Vega monorepo, its development is closely aligned with Vega's evolution, ensuring seamless integration. The current stable version of the Vega monorepo is v6.2.0, with vega-time benefiting from this active development cycle which includes regular minor and patch releases. A significant change in the v6 series is the transition to an ESM-only distribution, impacting how the library is imported and used in modern JavaScript environments. This package differentiates itself by offering robust, performant time utilities tailored for data visualization contexts, handling various time units and interval operations that are common in charting and graphing.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use core vega-time functions for formatting, parsing, flooring, and offsetting dates using various time intervals.

import { timeFormat, timeParse, timeFloor, timeMonth } from 'vega-time';

// Example 1: Formatting a date into a specific string format
const now = new Date();
const formattedDate = timeFormat('%Y-%m-%d')(now);
console.log(`Formatted date: ${formattedDate}`);
// Expected output: e.g., Formatted date: 2026-04-19 (or current date)

// Example 2: Parsing a date string into a Date object
const dateString = '2023-10-26 14:30:00';
const parse = timeParse('%Y-%m-%d %H:%M:%S');
const parsedDate = parse(dateString);
console.log(`Parsed date: ${parsedDate}`);
// Expected output: e.g., Parsed date: Thu Oct 26 2023 14:30:00 GMT...

// Example 3: Flooring a date to the beginning of the nearest month
const specificDate = new Date('2026-04-19T10:00:00Z');
const flooredToMonth = timeFloor(timeMonth)(specificDate);
console.log(`Date floored to month: ${flooredToMonth}`);
// Expected output: e.g., Date floored to month: Sat Apr 01 2026 00:00:00 GMT...

// Example 4: Offsetting a date by a specific time interval
const today = new Date('2026-04-19T12:00:00Z');
const nextMonth = timeMonth.offset(today, 1);
console.log(`Today: ${today.toISOString()}`);
console.log(`Next month: ${nextMonth?.toISOString()}`);
// Expected output: e.g., Next month: 2026-05-19T12:00:00.000Z

view raw JSON →