Skip to content
Agents tracked: 440 Downloads (7d): 202M down 19.5% GitHub stars: 10.4M Releases (7d): 607 Agent status: 3 with issuesSince last limit reset: Claude 28d · Codex 0d Updated Oct 21, 2026

Group dates by day in date-fns

Charts and logs often need events counted per day. Formatting each date as yyyy-MM-dd gives a key that is the same for every time on that day.

All date-fns examples date-fns: usage and stats

Group dates by day in date-fns: screenshot of the example below running
The code below, running in Chromium on our server (Oct 14, 2026). Press Run to see it live and change it.

How it works

  1. Events with times. Each event is a Date that includes hours and minutes. Several events fall on the same day. [1]
  2. Build a day key. format(d, "yyyy-MM-dd") turns each event into a day string. The code uses that string as a key in a plain object and counts the events. [1]
  3. Print the groups. The loop prints each day with its event count. The keys print in the order they were first seen in the array. [1]

Run and edit it

It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.

import { format } from "date-fns";

// new Date(year, monthIndex, day, hours, minutes)
const events = [
  new Date(2024, 2, 4, 9, 30),
  new Date(2024, 2, 4, 14, 5),
  new Date(2024, 2, 5, 8, 0),
  new Date(2024, 2, 4, 18, 45),
  new Date(2024, 2, 6, 11, 15),
  new Date(2024, 2, 5, 16, 20),
];

const perDay = {};
for (const e of events) {
  const day = format(e, "yyyy-MM-dd");
  perDay[day] = (perDay[day] || 0) + 1;
}

for (const [day, count] of Object.entries(perDay)) {
  console.log(`${day}: ${count} event(s)`);
}
Ctrl/⌘ + Enter runs

Free for signed-in members, with a daily limit. Describe the change in plain words; the new code runs only in your browser.

Add events with different hours on the same day and watch that day's count grow. Code written by AgentGid [1].

Checked Oct 14, 2026: it starts without errors in Chromium with date-fns 4.4.0. Needs WebGL.

More date-fns examples

Sort objects by a date field in date-fns Format a date as yyyy-MM-dd in date-fns Sort dates oldest first in date-fns Sort dates newest first in date-fns

Sources

  1. date-fns README (GitHub)

Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.

Building something with date-fns? Ask AgentGid →