Makes it easy to add, subtract, divide, test intersection and more on hh:mm-based time.
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
$denormalize(other: ): string
Possibly add a 24 hour offset if the input time is greater than itself, then convert to string.
$normalize(): 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
denormalize(other: ): HHMM
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(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
isDivisible(other: ): boolean
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 a time to nearest interval using flooring or rounding.
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.
parseToMinutes: (input: string) => number
Converts a string formatted hh:mm to the amount of minutes it represents.
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