npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@builtwithjavascript/countdown

v1.1.0

Published

Accurate countdown

Readme

@builtwithjavascript/countdown

An accurate and easy-to-use TypeScript package for managing countdown timers and displaying current time. This package provides a flexible Countdown class and utility functions to format the remaining time until a future date, as well as the current time.

npm version

Install

npm i @builtwithjavascript/countdown

How to use

import { 
  Countdown, 
  formatTimeRemaining, 
  formatCurrentTime,
  type TCountdownCallback,
} from '@builtwithjavascript/countdown'

// 1. Define your callback function that will receive the updated time
const countdownCallback: TCountdownCallback = (results) => {
  // In a real web browser environment, you would update a DOM element here.
  // For this example, we'll just log to the console.
  // We use the provided formatTimeRemaining, formatCurrentTime helper function to
  // format the results in a friendly format:
  const fornattedRemainingTime = formatTimeRemaining(results.timeRemaining)
  const furmattedCurrentTime = formatCurrentTime(results.currentTime)
  console.log(`   ${furmattedCurrentTime} (Current)`)
  console.log(`${fornattedRemainingTime} (Remaining)`)

  if (results.timeRemaining.isFinished) {
    console.log(`Countdown finished!`)
  }
}

// 2. Create an instance of the Countdown
const countdown = new Countdown(countdownCallback)

// 3. Define your future date
let targetDate = new Date('2025-11-25T19:45:00') // Example: Nov 25, 2025, 07:45 PM

// 4. Start the countdown
countdown.start(targetDate)

// 5. optional: if needed, later can be stopped:
// countdown.stop()

// 6. optional: if needed, stop and restart with a new targetDate
// countdown.stop()
// targetDate = new Date('2026-07-23T08:15:00')
// countdown.start(targetDate)

Project Structure and Code Description

The package is structured to provide clear separation of concerns, making it easy to understand, use, and extend.

├── src/
│  ├── countdown/
│  │  ├── types.ts
│  │  ├── countdown.ts
│  │  ├── formatters.ts
│  │  └── index.ts
│  └── index.ts
└── README.md
  • src/countdown/types.ts: This file defines all the TypeScript types used throughout the package.

    • TCurrentTime: Represents a standard time structure with year, month, day, hours, minutes, and seconds.
    • TTimeRemaining: Represent the reamining time structure with day, hours, minutes, seconds, totalMilliseconds and isFinished, specifically for the countdown's remaining time.
    • TRemainingTimeResult: The primary return type for the countdown callback, containing both timeRemaining and currentTime objects.
    • TCountdownCallback: Defines the signature for the callback function that consumers will provide to receive countdown updates.
  • src/countdown/countdown.ts: This is the core logic file containing the Countdown class.

    • Countdown class:
      • constructor(callback: TCountdownCallback): Initializes the countdown with a user-defined callback function.
      • private calculateRemainingTime(futureDate: Date, now: Date): TRemainingTimeResult: A private helper method that calculates the difference between futureDate and now, returning a structured TRemainingTimeResult object. It uses Math.ceil for seconds to ensure the countdown hits zero precisely.
      • public start(futureDate: Date): void: Initiates the countdown. It uses window.setTimeout for precise, second-aligned updates, calculating the delay until the start of the next second to avoid drift. The provided callback is invoked repeatedly with the latest time data.
      • public stop(): void: Stops the currently running countdown by clearing the setTimeout interval.
  • src/countdown/formatters.ts: This file contains utility functions for formatting the time objects into human-readable strings.

    • formatTimeRemaining(time: TTimeRemaining): string: Takes a TTimeRemaining object and formats it into a DD:HH:MM:SS string. It handles carry-overs for seconds, minutes, and hours to ensure correct display.
    • formatCurrentTime(time: TCurrentTime): string: Takes a TCurrentTime object and formats it into an yyyy-mm-dd hh:mm:ss string, suitable for displaying the current time.
  • src/countdown/index.ts: This file serves as the main entry point for the countdown module, re-exporting all necessary types, classes, and functions from types.ts, countdown.ts, and formatters.ts.

  • src/index.ts: The top-level entry point for the entire @builtwithjavascript/countdown package, simply re-exporting everything from src/countdown/index.ts.