Utils (@pim.sk/utils)

date-time

Date and time utilities — parsing, formatting, modifying and comparing dates. All functions work in local timezone without UTC drift.
import {toDate, now, convertSkToIso, modifyDate, betweenDateTime, isInTime, firstDate, lastDate, difference, betweenMonths, durationFormat, timeElapsed} from '@pim.sk/utils/date-time.mjs'

Usage

now()

Returns the current datetime string in local timezone (ISO format without UTC shift).

// no input
const current = now()

toDate(d, rType)

Convert a date/timestamp to a specific format. rType: 'date' | 'datetime' | 'datetime-local' | 'month' | 'year' | 'time' | 'ymd' | 'number'

"2026-04-11 14:30:00"
toDate(d, "date")           // 2026-04-11
toDate(d, "datetime")       // 2026-04-11T14:30:00
toDate(d, "datetime-local") // 2026-04-11T14:30
toDate(d, "month")          // 2026-04
toDate(d, "year")           // 2026
toDate(d, "time")           // 14:30:00
toDate(d, "ymd")            // 20260411
toDate(d, "number")         // timestamp ms

convertSkToIso(dateString)

Convert Slovak date format (D.M.YYYY) to ISO format (YYYY-MM-DD).

1.2.2026 13:56 | 1.2.2026
const iso = convertSkToIso("1.2.2026 13:56")

modifyDate(date, value, unit)

Add or subtract from a date. unit: 'y' | 'm' | 'd' | 'H' | 'i' | 's'

"2026-04-11"   value: ±N   unit: y|m|d|H|i|s
modifyDate(d, +1, "m")   // next month
modifyDate(d, -1, "m")   // prev month
modifyDate(d, +7, "d")   // +7 days

firstDate(d) / lastDate(d)

Get the first or last day of the month for a given date.

"2026-04-11"
const first = firstDate("2026-04-11")   // 2026-04-01
const last  = lastDate("2026-04-11")    // 2026-04-30

betweenDateTime(dTest, dFrom, dTo)

Check if a date is within a range. dFrom or dTo can be null (open-ended).

dTest="2026-04-11"
dFrom="2026-01-01"
dTo="2026-12-31"
betweenDateTime("2026-04-11", "2026-01-01", "2026-12-31")

isInTime(compareDateTime, nowDateTime?, limitTime?)

Check if a datetime is within N seconds of now (default: 6 hours = 21600s).

compareDateTime
limitTime = 21600 (6h)
isInTime(compareDateTime)            // within 6h of now
isInTime(compareDateTime, null, 60)  // within 60s of now

difference(date1, date2)

Returns the difference between two dates in milliseconds (date1 - date2).

"2026-04-11"  vs  "2026-01-01"
const ms   = difference("2026-04-11", "2026-01-01")
const days = ms / 1000 / 60 / 60 / 24

betweenMonths(d1, d2)

Returns the number of months between two dates (inclusive).

"2026-01-01"  vs  "2026-04-11"
const months = betweenMonths("2026-01-01", "2026-04-11")

durationFormat(seconds)

Format a duration (in seconds) into a human-readable string (e.g. "2m 5d 03:20:10").

90 | 3661 | 1234567 | -600
durationFormat(90)       // 01:30
durationFormat(3661)     // 01:01:01
durationFormat(1234567)  // 2w 0d 06:56:07

timeElapsed(secs, maxUnit?)

Format elapsed seconds into compact readable string. maxUnit: 'y'|'w'|'d'|'h'|'m'|'s' (default 's').

3661 | 90061 | 1234567
timeElapsed(3661)         // 1h 1m 1s
timeElapsed(3661, "m")    // 1h 1m
timeElapsed(1234567)      // 2w 0d 6h 56m 7s
v 1.1.2