API Reference
Complete API reference for Qrono.
Factory
All Factory methods accept an optional context object as the first argument to configure how the instance handles timezone and DST settings.
qrono(...args)
Creates a new Qrono datetime instance.
import { qrono } from 'qrono'
// Current time
qrono()
// From Date object
qrono(new Date())
// From timestamp (milliseconds)
qrono(1704067200000)
// From ISO string
qrono('2024-01-15T10:30:00.000Z')
// From components (year, month, day, hour, minute, second, millisecond)
qrono(2024, 1, 15, 10, 30, 0, 0)
// From array
qrono([2024, 1, 15, 10, 30])
// From object
qrono({ year: 2024, month: 1, day: 15 })
// With context options (context as first argument)
qrono({ localtime: true }, '2024-01-15')
// Context options are applied to the instance
qrono({ localtime: true, ambiguousAsDst: false }, '2024-01-15')qrono.date(...args)
Creates a new QronoDate instance (date only, no time component).
// Today
qrono.date()
// From string
qrono.date('2024-01-15')
// From components
qrono.date(2024, 1, 15)
// From array
qrono.date([2024, 1, 15])
// From object
qrono.date({ year: 2024, month: 1, day: 15 })valid()
Check if the instance represents a valid date.
qrono('2024-01-15').valid() // true
qrono(new Date('invalid')).valid() // falseConversion
toString()
Get the ISO 8601 string representation.
time.toString() // "2024-06-15T14:30:00.000Z"
qrono.date('2024-06-15').toString() // "2024-06-15"numeric()
Get the Unix timestamp in milliseconds.
time.numeric() // 1718458200000valueOf()
Same as numeric(). Allows using +time syntax.
+time // 1718458200000toArray()
Get an array of components.
time.toArray() // [2024, 6, 15, 14, 30, 0, 0]
qrono.date('2024-06-15').toArray() // [2024, 6, 15]toObject()
Get an object with named properties.
time.toObject()
// { year: 2024, month: 6, day: 15, hour: 14, minute: 30, second: 0, millisecond: 0 }
qrono.date('2024-06-15').toObject()
// { year: 2024, month: 6, day: 15 }nativeDate()
Get a native JavaScript Date object.
time.nativeDate() // Date instancetoDate()
Get a QronoDate instance (date portion only).
time.toDate() // QronoDate instancetoDatetime()
Converts a QronoDate to a Qrono datetime at midnight.
qrono.date('2024-06-15').toDatetime().toString()
// "2024-06-15T00:00:00.000Z"Constants
Day of Week
qrono.monday // 1
qrono.tuesday // 2
qrono.wednesday // 3
qrono.thursday // 4
qrono.friday // 5
qrono.saturday // 6
qrono.sunday // 7Accessors
All component methods work as both getters (no argument) and setters (with argument). Setters return a new instance (immutable).
year()
const time = qrono('2024-06-15')
time.year() // 2024 (getter)
time.year(2025) // New instance with year 2025month()
time.month() // 6 (getter, 1-12)
time.month(12) // New instance with month 12day()
time.day() // 15 (getter)
time.day(20) // New instance with day 20hour()
time.hour() // 14 (getter, 0-23)
time.hour(10) // New instance with hour 10minute()
time.minute() // 30 (getter, 0-59)
time.minute(45) // New instance with minute 45second()
time.second() // 45 (getter, 0-59)
time.second(30) // New instance with second 30millisecond()
time.millisecond() // 123 (getter, 0-999)
time.millisecond(500) // New instance with millisecond 500offset()
Get the timezone offset in minutes.
qrono().asUtc().offset() // 0
qrono().asLocaltime().offset() // e.g., 540 (JST)Context
qrono.context(options)
Sets the default context for all new instances.
qrono.context({ localtime: true, ambiguousAsDst: false })Parameters:
localtime-boolean- Use local time instead of UTCambiguousAsDst-boolean- Interpret ambiguous times as DST
qrono.asUtc()
Sets the default context to UTC mode and returns the qrono function for chaining.
qrono.asUtc()
const utcTime = qrono('2024-01-15')
utcTime.localtime() // falseqrono.asLocaltime()
Sets the default context to local time mode and returns the qrono function for chaining.
qrono.asLocaltime()
const localTime = qrono('2024-01-15')
localTime.localtime() // trueqrono.localtime(value)
Sets and returns the qrono default localtime setting.
qrono.localtime(true)
qrono.localtime() // truecontext()
Get or set context options.
// Get current context
time.context() // { localtime: false, ambiguousAsDst: false }
// Set context (returns new instance)
time.context({ localtime: true })localtime()
Get or set localtime mode.
time.localtime() // Get: true or false
time.localtime(true) // Set: returns new instanceambiguousAsDst()
Get or set ambiguous DST handling.
time.ambiguousAsDst() // Get: true or false
time.ambiguousAsDst(true) // Set: returns new instanceasUtc()
Convert to UTC mode.
const utc = time.asUtc()
utc.localtime() // falseasLocaltime()
Convert to local time mode.
const local = time.asLocaltime()
local.localtime() // trueCalculation
plus(duration)
Add time to the date.
time.plus({ year: 1 })
time.plus({ month: 2, day: 15 })
time.plus({ hour: 5, minute: 30, second: 45, millisecond: 500 })minus(duration)
Subtract time from the date.
time.minus({ year: 1 })
time.minus({ month: 2, day: 15 })Comparison Methods
isSame(other)
Check if two dates are equal.
time.isSame(qrono('2024-01-15')) // true or false
time.isSame(new Date())
time.isSame(1704067200000)isBefore(other)
Check if this date is before another.
time.isBefore(qrono('2024-12-31')) // true or falseisAfter(other)
Check if this date is after another.
time.isAfter(qrono('2024-01-01')) // true or falseisSameOrBefore(other)
Check if this date is the same or before another.
time.isSameOrBefore(qrono('2024-12-31')) // true or falseisSameOrAfter(other)
Check if this date is the same or after another.
time.isSameOrAfter(qrono('2024-01-01')) // true or falseisBetween(start, end)
Check if this date is between two dates (inclusive).
time.isBetween(qrono('2024-01-01'), qrono('2024-12-31')) // true or falseStart Of Methods
startOfYear()
Get the start of the year.
qrono('2024-06-15 14:30:00').startOfYear()
// 2024-01-01T00:00:00.000ZstartOfMonth()
Get the start of the month.
qrono('2024-06-15 14:30:00').startOfMonth()
// 2024-06-01T00:00:00.000ZstartOfDay()
Get the start of the day.
qrono('2024-06-15 14:30:00').startOfDay()
// 2024-06-15T00:00:00.000ZstartOfHour()
Get the start of the hour.
qrono('2024-06-15 14:30:45').startOfHour()
// 2024-06-15T14:00:00.000ZstartOfMinute()
Get the start of the minute.
qrono('2024-06-15 14:30:45').startOfMinute()
// 2024-06-15T14:30:00.000ZstartOfSecond()
Get the start of the second.
qrono('2024-06-15 14:30:45.123').startOfSecond()
// 2024-06-15T14:30:45.000ZEnd Of Methods
endOfYear()
Returns the last day of the year.
qrono.date('2024-06-15').endOfYear().toString() // "2024-12-31"endOfMonth()
Returns the last day of the month.
qrono.date('2024-06-15').endOfMonth().toString() // "2024-06-30"Date Information
dayOfWeek()
Get the day of the week (Monday = 1, Sunday = 7).
qrono('2024-06-15').dayOfWeek() === qrono.saturday // 6dayOfYear()
Get the day of the year (1-366).
qrono('2024-06-15').dayOfYear() // 167weekOfYear()
Get the ISO week number.
qrono('2024-06-15').weekOfYear() // 24yearOfWeek()
Get the year of the ISO week (may differ at year boundaries).
qrono('2025-12-29').yearOfWeek() // 2026isLeapYear()
Check if the year is a leap year.
qrono('2024-01-01').isLeapYear() // truedaysInMonth()
Get the number of days in the current month.
qrono('2024-02-15').daysInMonth() // 29daysInYear()
Get the number of days in the current year.
qrono('2024-01-01').daysInYear() // 366weeksInYear()
Get the number of ISO weeks in the current year (52 or 53).
qrono('2024-01-01').weeksInYear() // 52DST Methods
hasDstInYear()
Check if the year has daylight saving time transitions.
qrono.asLocaltime()
qrono(1950, 1, 1).hasDstInYear() // true
qrono(2024, 1, 1).hasDstInYear() // false (in Japan)isInDst()
Check if the current time is in daylight saving time.
qrono.asLocaltime()
qrono('1950-09-10 00:59:59').isInDst() // trueisDstTransitionDay()
Check if the current day has a DST transition.
qrono.asLocaltime()
qrono('1950-05-07').isDstTransitionDay() // trueminutesInDay()
Get the number of minutes in the current day (accounts for DST).
qrono.asLocaltime()
qrono('1950-05-06').minutesInDay() // 1440
qrono('1950-05-07').minutesInDay() // 1380 (DST spring forward)
qrono('1950-09-10').minutesInDay() // 1500 (DST fall back)