@bemoje/date
v2.0.0
Published
Lightweight date and time utilities for formatting, time-unit conversion, and duration measurement.
Maintainers
Readme
@bemoje/date
Lightweight date and time utilities for formatting, time-unit conversion, and duration measurement.
Exports
- Timer: Returns a function that returns the elapsed time since invokation.
- dateString: Reutnrs the date formatted as: yyyy-MM-dd
- daysToMs: Converts days to milliseconds.
- getWeek: Get the week number of the year for a given date using Danish locale.
- hasCooldownElapsed: Determines if a specified cooldown period has elapsed since a given date.
- hoursToMs: Converts hours to milliseconds.
- minutesToMs: Converts minutes to milliseconds.
- monthNameDa: Returns the name, in Danish language, of the month corresponding to the provided month number.
- monthNameDaRelative name of the month relative to the current month.
- msSinceDate: Calculates the number of milliseconds that have elapsed since the given date.
- secondsToMs: Converts seconds to milliseconds.
- stripTime: Remove the time component from a date, returning only the date part.
- today: Get the UTC date today, time stripped
- yesterday: Get the UTC date yesterday, time stripped
Installation
npm install @bemoje/dateUsage
Date Formatting
import { dateString, today, yesterday } from '@bemoje/date'
dateString()
// => '2026-02-28'
dateString(new Date(2025, 0, 15))
// => '2025-01-15'
today()
// => Date object for today at 00:00:00
yesterday()
// => Date object for yesterday at 00:00:00Time Unit Conversion
import { daysToMs, hoursToMs, minutesToMs, secondsToMs } from '@bemoje/date'
daysToMs(1) // => 86400000
hoursToMs(2) // => 7200000
minutesToMs(30) // => 1800000
secondsToMs(45) // => 45000Duration & Cooldowns
import { msSinceDate, hasCooldownElapsed, Timer } from '@bemoje/date'
// Milliseconds elapsed since a date
const start = new Date('2026-02-01')
msSinceDate(start)
// => number of ms since Feb 1
// Check if a cooldown period has passed
hasCooldownElapsed(start, daysToMs(7))
// => true (if more than 7 days have passed)
// Stopwatch-style timer
const elapsed = Timer()
// ... do some work ...
elapsed()
// => '142ms' (human-readable duration string)Week & Date Operations
import { getWeek, stripTime } from '@bemoje/date'
getWeek(new Date('2026-02-28'))
// => 9
// Remove time component from a Date
stripTime(new Date('2026-02-28T15:30:00'))
// => Date('2026-02-28T00:00:00')Danish Month Names
import { monthNameDa, monthNameDaRelative } from '@bemoje/date'
monthNameDa(new Date('2026-03-15'))
// => 'Marts'
monthNameDaRelative(new Date())
// => relative month name in Danish