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)
class HHMM

Makes it easy to add, subtract, divide, test intersection and more on hh:mm-based time.

Constructors

new
HHMM(hhmm: )

Methods

Add two times together, then convert to string. Note that times over "23:59" will be modulo'd back to "00:00" when calling toString

Possibly add a 24 hour offset if the input time is greater than itself, then convert to string.

Normalize the time to be within 00:00 - 23:59, then convert to string. The result is exactly the same as calling .toString(), but the normalize function name might be clearer than .toString(), but that's just a guess.

"23:00"[HM].$normalize()
// ^? HHMM { minutes = 1380 }
"24:00"[HM].normalize()
// ^? HHMM { minutes = 0 }
"25:00"[HM].normalize()
// ^? HHMM { minutes = 60 }

Subtract one time from another, then convert to string. Note that times over "23:59" will be modulo'd back to "00:00" when calling toString

[Symbol.toPrimitive](hint:
"number"
| "string"
| "default"
): string | number
add(other: ): HHMM

Add two times together

Possibly add a 24 hour offset if the input time is greater than itself.

Divide a time with another time, and get the quotient

eq = equal

Given another time, check if this and the input time represent the same time.

If you're using the HM symbol property, you most likely don't want to use this function. You will be dealing with plain strings, so just check whether one string equals the other string.

If you're working with HHMM objects, you must use this function to compare equality of two HHMM objects. It's not possible to override == and === in javascript. If you compare with ===, you end up comparing object references, instead of the value they represent.

const time = new HHMM("20:00")
time.eq(time)
time.eq("24:00")
time.eq(600) // comparing minutes. 600 minutes = 10:00

gt = greater than

Checks whether this represents a time later than the input time

It's recommended to use the HM symbol property, but there are multiple ways to do the comparison.

const time1 = "08:00"
const time2 = "09:00"

// ✅ Most ergonomic and readable
if (time1[HM] > time2[HM]) 

// 🟧 Not a bad solution either, but at-a-glance readability suffers
if (time1[HM].gt(time2))

// 🟧 Works, but there's 6 more characters to read and write for each `new HHMM()` compared
// to using the HM symbol property. Multiply that with the number of times
// you are doing any comparison, add/subtract etc.
if (new HHMM(time1) > new HHMM(time2))

// 🟥 Worst of both worlds
if (new HHMM(time1).gt(time2))

gte = greater than or equal

Checks whether this represents a time later than or equal the input time

It's recommended to use the HM symbol property, but there are multiple ways to do the comparison.

const time1 = "08:00"
const time2 = "09:00"

// ✅ Most ergonomic and readable
if (time1[HM] >= time2[HM]) 

// 🟧 Not a bad solution either, but at-a-glance readability suffers
if (time1[HM].gte(time2))

// 🟧 Works, but there's 6 more characters to read and write for each `new HHMM()` compared
// to using the HM symbol property. Multiply that with the number of times
// you are doing any comparison, add/subtract etc.
if (new HHMM(time1) >= new HHMM(time2))

// 🟥 Worst of both worlds
if (new HHMM(time1).gte(time2))
intersects(
from: ,
to: ,
options?: { from: "inclusive" | "exclusive"; to: "inclusive" | "exclusive"; },
): boolean

Checks if this is a time between two other times [start, end). By default, the check is inclusive on the start, and exclusive on the end. In other words:

Generate all intervals between a start time and end time. If end is less than start, generate all intervals between start and end + 24h

Checks if other is divisible by this without remainder

lt = less than

Checks whether this represents a time earlier than the input time

It's recommended to use the HM symbol property, but there are multiple ways to compare less than.

const time1 = "08:00"
const time2 = "09:00"

// ✅ Most ergonomic and readable
if (time1[HM] < time2[HM]) 

// 🟧 Not a bad solution either, but at-a-glance readability suffers
if (time1[HM].lt(time2))

// 🟧 Works, but there's 6 more characters to read and write for each `new HHMM()` compared
// to using the HM symbol property. Multiply that with the number of times
// you are doing any comparison, add/subtract etc.
if (new HHMM(time1) < new HHMM(time2))

// 🟥 Worst of both worlds
if (new HHMM(time1).lt(time2))

lte = less than or equal

Checks whether this represents a time earlier than or equal the input time

It's recommended to use the HM symbol property, but there are multiple ways to compare less than.

const time1 = "08:00"
const time2 = "09:00"

// ✅ Most ergonomic and readable
if (time1[HM] < time2[HM]) 

// 🟧 Not a bad solution either, but at-a-glance readability suffers
if (time1[HM].lt(time2))

// 🟧 Works, but there's 6 more characters to read and write for each `new HHMM()` compared
// to using the HM symbol property. Multiply that with the number of times
// you are doing any comparison, add/subtract etc.
if (new HHMM(time1) < new HHMM(time2))

// 🟥 Worst of both worlds
if (new HHMM(time1).lt(time2))

Normalize the time to be within 00:00 - 23:59.

"23:00"[HM].normalize()
// ^? HHMM { minutes = 1380 }
"24:00"[HM].normalize()
// ^? HHMM { minutes = 0 }
"25:00"[HM].normalize()
// ^? HHMM { minutes = 60 }
round(
toNearestInterval: ,
method?: "round" | "floor",
): HHMM

Round a time to nearest interval using flooring or rounding.

Subtract one time from another.

This method is used by JSON.stringify(), it's not intended for you to use. You can use .toString(false) instead. Try to avoid as much as possble passing HHMM objects around your program, and keep all of them as plain strings. It will make your life a lot less miserable.

toString(normalize?: boolean): string

Converts the HHMM instance to a string. By default the time is normalized to be between "00:00" and "23:59", since the library optimizes for displaying to end users.

Static Properties

private
parseToMinutes: (input: string) => number

Converts a string formatted hh:mm to the amount of minutes it represents.

Static Methods

intersects(
fromA: ,
toA: ,
fromB: ,
toB: ,
): boolean

Checks if [fromA, toA) intersects [fromB, toB)

Create an HHMM object based on the current time with tz offset. if UTC time is 10:00 and the caller lives in UTC+2, then .now() returns 12:00

The comparison function used in sorting

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 { HHMM } from "@revosw/hhmm";
or

Import directly with a jsr specifier

import { 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 { 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 { HHMM } from "@revosw/hhmm";

Add Package

vlt install jsr:@revosw/hhmm

Import symbol

import { HHMM } from "@revosw/hhmm";

Add Package

npx jsr add @revosw/hhmm

Import symbol

import { HHMM } from "@revosw/hhmm";

Add Package

bunx jsr add @revosw/hhmm

Import symbol

import { HHMM } from "@revosw/hhmm";