Skip to main content
Home

latest

Utility for doing math on strings in the format "hh:mm", such as adding, subtracting, intersection testing and modulo

This package works with Node.js, Deno, Bun, BrowsersIt is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
94%
Published
a year ago (0.1.2)

hhmm library logo

An easy and serialization-friendly way to handle HH:MM calculations

Here are some motivating examples for this library.

// Using DateTime
const 

if you'd like to check equality and inequality

This library makes it easy to do the following:

  • Adding and subtracting times
// Using class instances
const add1 = new HHMM("08:00").add("01:00")
    // ^? = HHMM { minute = 540 }
const sub1 = new HHMM("08:00").subtract("00:15")
    // ^? = HHMM { minute = 465 }

// Using the HM symbol property
const add2 = "08:00"[HM].add("09:00")
const sub2 = "08:00"[HM].subtract("09:00")
  • Divide
// Using class instances
const newTime = new HHMM("08:00").subtract("09:00")

// Using the HM symbol property
const newTime = "08:00"[HM].subtract("09:00")

Symbol property mode

To start using the HM symbol property, the String interface should be augmented to get proper typescript types.

// global.d.ts
declare global {
    interface String {
        [HM]: HHMM;
    }
}

In addition, you must import @revosw/hhmm/symbol. Importing this file has the side effect of adding the HM symbol as a property on the String.prototype object.

import "@revosw/hhmm/symbol"

You will suffer if you try to

It is tiresome having to create class instances all the time for the simplest things.

const from = new HHMM("08:00")
const to = new HHMM("09:00")

if (from.lt(to)) {
    // from < to
}

if (new HHMM("08:00") < new HHMM("09:00"))

Problems with structuredClone()

Since class instances

Problems with React

Problems with TanStack Query

The query cache does not like

While it's possible to use the

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@revosw/hhmm

Import symbol

import * as hhmm from "@revosw/hhmm";
or

Import directly with a jsr specifier

import * as hhmm from "jsr:@revosw/hhmm";

Add Package

pnpm i jsr:@revosw/hhmm
or (using pnpm 10.8 or older)
pnpm dlx jsr add @revosw/hhmm

Import symbol

import * as hhmm from "@revosw/hhmm";

Add Package

yarn add jsr:@revosw/hhmm
or (using Yarn 4.8 or older)
yarn dlx jsr add @revosw/hhmm

Import symbol

import * as hhmm from "@revosw/hhmm";

Add Package

npx jsr add @revosw/hhmm

Import symbol

import * as hhmm from "@revosw/hhmm";

Add Package

bunx jsr add @revosw/hhmm

Import symbol

import * as hhmm from "@revosw/hhmm";