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 i @builtwithjavascript/countdown
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)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 withyear,month,day,hours,minutes, andseconds.TTimeRemaining: Represent the reamining time structure withday,hours,minutes,seconds,totalMillisecondsandisFinished, specifically for the countdown's remaining time.TRemainingTimeResult: The primary return type for the countdown callback, containing bothtimeRemainingandcurrentTimeobjects.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 theCountdownclass.Countdownclass: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 betweenfutureDateandnow, returning a structuredTRemainingTimeResultobject. It usesMath.ceilfor seconds to ensure the countdown hits zero precisely.public start(futureDate: Date): void: Initiates the countdown. It useswindow.setTimeoutfor 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 thesetTimeoutinterval.
-
src/countdown/formatters.ts: This file contains utility functions for formatting the time objects into human-readable strings.formatTimeRemaining(time: TTimeRemaining): string: Takes aTTimeRemainingobject and formats it into aDD:HH:MM:SSstring. It handles carry-overs for seconds, minutes, and hours to ensure correct display.formatCurrentTime(time: TCurrentTime): string: Takes aTCurrentTimeobject and formats it into anyyyy-mm-dd hh:mm:ssstring, suitable for displaying the current time.
-
src/countdown/index.ts: This file serves as the main entry point for thecountdownmodule, re-exporting all necessary types, classes, and functions fromtypes.ts,countdown.ts, andformatters.ts. -
src/index.ts: The top-level entry point for the entire@builtwithjavascript/countdownpackage, simply re-exporting everything fromsrc/countdown/index.ts.