Utility for doing math on strings in the format "hh:mm", such as adding, subtracting, intersection testing and modulo
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
Add Package
deno add jsr:@revosw/hhmm
Import symbol
import * as hhmm from "@revosw/hhmm";
Import directly with a jsr specifier
import * as hhmm from "jsr:@revosw/hhmm";
Add Package
pnpm i jsr:@revosw/hhmm
pnpm dlx jsr add @revosw/hhmm
Import symbol
import * as hhmm from "@revosw/hhmm";
Add Package
yarn add jsr:@revosw/hhmm
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";