API Reference
Complete API reference for Qrono.
The library provides two classes: Qrono, which represents a point in time, and QronoDate, which represents a calendar date.
- Factory
- qrono(...args) static 14 overloads
- qrono.date(...args) static 7 overloads
- Conversion
- .toString() Qrono QronoDate
- .valueOf() Qrono QronoDate
- .toArray() Qrono QronoDate
- .toObject() Qrono QronoDate
- .nativeDate() Qrono QronoDate
- .toDate() Qrono
- .toDatetime() QronoDate
- Constants
- qrono.monday static
- qrono.tuesday static
- qrono.wednesday static
- qrono.thursday static
- qrono.friday static
- qrono.saturday static
- qrono.sunday static
- Accessors
- Context
- qrono.context() static 2 overloads
- .context() Qrono 2 overloads
- Calculation
- .plus(duration) Qrono 4 overloads QronoDate 4 overloads
- .minus(duration) Qrono 4 overloads QronoDate 4 overloads
- .valid() Qrono QronoDate
- Comparison
- .isSame(other) Qrono 3 overloads QronoDate 3 overloads
- .isBefore(other) Qrono 3 overloads QronoDate 3 overloads
- .isAfter(other) Qrono 3 overloads QronoDate 3 overloads
- .isSameOrBefore(other) Qrono 3 overloads QronoDate 3 overloads
- .isSameOrAfter(other) Qrono 3 overloads QronoDate 3 overloads
- .isBetween(start, end) Qrono 3 overloads QronoDate 3 overloads
- Time Unit Boundary
- .startOfYear() Qrono QronoDate
- .startOfMonth() Qrono QronoDate
- .startOfDay() Qrono
- .startOfHour() Qrono
- .startOfMinute() Qrono
- .startOfSecond() Qrono
- .endOfYear() QronoDate
- .endOfMonth() QronoDate
- Date Information
- .dayOfWeek() Qrono QronoDate
- .dayOfYear() Qrono QronoDate
- .weekOfYear() Qrono QronoDate
- .yearOfWeek() Qrono QronoDate
- .isLeapYear() Qrono QronoDate
- .daysInMonth() Qrono QronoDate
- .daysInYear() Qrono QronoDate
- .weeksInYear() Qrono QronoDate
- Daylight Saving Time
- .hasOffsetChangeInYear() Qrono QronoDate
- .isInDst() Qrono QronoDate
- .hasOffsetChangeInDay() Qrono QronoDate
- .minutesInDay() Qrono QronoDate
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 string
//
// Conforms to ISO 8601 format except for a few exceptions.
// If no time zone is specified, parsing is performed
// according to the current context (`localtime`).
//
// Format:
// yyyy[[-|/]MM[[-|/]DD]][(T| )HH[:]mm[[:]ss[(.|:)SSS]]][Z|(+|-)hh:mm]
//
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')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 })Conversion
toString()
Get the ISO 8601 string representation.
time.toString() // "2024-06-15T14:30:00.000Z"
qrono.date('2024-06-15').toString() // "2024-06-15"valueOf()
Get the numeric value of the instance.
For Qrono, the unit is milliseconds since UNIX epoch.
For QronoDate, the unit is days since UNIX epoch.
+time // 1718458200000
+qrono.date('1970-01-02') // 1toArray()
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
These definitions follow ISO 8601, where Monday is 1 and Sunday is 7.
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()
Returns the time zone offset of the Qrono instance in minutes.
A positive value indicates that the base time is ahead of UTC, and a negative value indicates that it is behind UTC.
TIP
This is the opposite sign convention of JavaScript's Date.prototype.getTimezoneOffset.
qrono().context({ localtime: false }).offset() // 0
qrono().context({ localtime: true }).offset() // e.g., 540 (JST)Context
qrono.context(options)
Sets the default context for all new instances.
qrono.context({ localtime: true, disambiguation: 'earlier' })Parameters:
localtime-boolean- Use local time instead of UTCdisambiguation-'compatible' | 'earlier' | 'later' | 'reject'- How to resolve ambiguous local times at DST transitions (default:'compatible')Option Gap (spring-forward) Overlap (fall-back) 'compatible'(default)Later (DST side) Earlier (DST side) 'earlier'Earlier (standard side) Earlier (DST side) 'later'Later (DST side) Later (standard side) 'reject'Throws RangeErrorThrows RangeError
context()
Get or set context options.
// Get current context
time.context() // { localtime: false, disambiguation: 'compatible' }
// Set context (returns new instance)
time.context({ localtime: true })Calculation
When a Qrono instance is cast to a number, it represents the milliseconds since the UNIX epoch, just like a Date object. A QronoDate instance uses “days” as its unit. Therefore, expressions like the following work as expected.
qrono('2021-08-31 12:34') < qrono('2021-09-30 12:34')
const today = qrono.date('2021-08-31')
const tomorrow = qrono.date(today + 1)
tomorrow - today === 1plus(duration)
Add time to the date.
In operations using Object, year, month, and day are calculated literally.
For example, adding one month at the end of a month results in the end of the following month.hour, minute, second, and millisecond are treated as a duration in calculations.
time.plus({ month: 2, day: 15 })
time.plus({ hour: 5, minute: 30, second: 45, millisecond: 500 })
time.plus(3600 * 1000) // add one hour via milliseconds
time.plus([0, 1, 1, 4]) // add 1 month, 1 day, 4 hours
time.plus({ minute: 15, second: 30 }) // add minutes + secondsQronoDate variants also accept:
qrono.date('2024-01-01').plus(1) // +1 day
qrono.date('2024-01-01').plus([0, 1, 1]) // +1 month, +1 day
qrono.date('2024-01-01').plus({ year: 1 }) // same object formminus(duration)
Subtract time from the date.
In operations using Object, year, month, and day are calculated literally.
For example, subtracting { month: 1 } from July 31 results in June 28.hour, minute, second, and millisecond are treated as a duration in calculations.
time.minus({ year: 1 })
time.minus({ month: 2, day: 15 })
time.minus({ hour: 5, minute: 30 })
time.minus(15 * 60 * 1000) // subtract 15 minutes via milliseconds
time.minus([0, 0, 0, 6]) // subtract 6 hours
time.minus({ day: 1, minute: 45 }) // subtract days + minutesQronoDate variants also accept:
qrono.date('2024-01-05').minus(2) // -2 days
qrono.date('2024-01-05').minus([0, 0, 5]) // -5 days
qrono.date('2024-01-05').minus({ month: 1 }) // subtract monthvalid()
Check if the instance represents a valid date.
qrono('2024-01-15').valid() // true
qrono(new Date('invalid')).valid() // falseComparison
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 falseTime Unit Boundary
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.000ZendOfYear()
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() // 52Daylight Saving Time
hasOffsetChangeInYear()
Check if the year has daylight saving time transitions.
qrono.context({ localtime: true })
qrono(1950, 1, 1).hasOffsetChangeInYear() // true
qrono(2024, 1, 1).hasOffsetChangeInYear() // false (in Japan)isInDst()
Check if the current time is in daylight saving time.
qrono.context({ localtime: true })
qrono('1950-09-10 00:59:59').isInDst() // truehasOffsetChangeInDay()
Check if the current day has a DST transition.
qrono.context({ localtime: true })
qrono('1950-05-07').hasOffsetChangeInDay() // trueminutesInDay()
Get the number of minutes in the current day (accounts for DST).
qrono.context({ localtime: true })
qrono('1950-05-06').minutesInDay() // 1440
qrono('1950-05-07').minutesInDay() // 1380 (DST spring forward)
qrono('1950-09-10').minutesInDay() // 1500 (DST fall back)