Fundamental Operations
Like calculus provides dx and ∫, we provide period, divide, merge, and navigate. Everything else emerges through composition.
A minimal time library with fundamental operations that compose into powerful temporal manipulations
npm install usetemporalpnpm add usetemporalyarn add usetemporalimport { createTemporal, usePeriod, divide } from 'usetemporal'
const temporal = createTemporal({ date: new Date() })
const month = usePeriod(temporal, 'month')
const days = divide(temporal, month.value, 'day')Like calculus provides fundamental operations (dx, ∫) that compose into powerful tools, useTemporal provides fundamental time operations that compose into complex temporal manipulations.
// Fundamental operations - like dx and ∫
period(temporal, date, unit) // Create any period
divide(temporal, period, unit) // Hierarchical subdivision
merge(temporal, periods) // Combine periods
next/previous/go // Relative navigation
// Everything else is composition
const daysInMonth = (month) => divide(temporal, month, 'day')
const businessDays = (period) => divide(temporal, period, 'day')
.filter(day => ![0, 6].includes(day.date.getDay()))Traditional Approach - Manual and error-prone:
const startOfMonth = new Date(date.getFullYear(), date.getMonth(), 1)
const endOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0)
const days = []
for (let d = new Date(startOfMonth); d <= endOfMonth; d.setDate(d.getDate() + 1)) {
days.push(new Date(d))
}useTemporal - Elegant and reactive:
const month = usePeriod(temporal, 'month')
const days = divide(temporal, month.value, 'day')
// Automatically updates when month changesconst year = usePeriod(temporal, 'year')
const months = divide(temporal, year.value, 'month')
const days = divide(temporal, months[0], 'day')
const hours = divide(temporal, days[0], 'hour')import { watch } from '@vue/reactivity'
watch(month, (newMonth) => {
console.log('Month changed:', newMonth.date)
})import { defineUnit } from 'usetemporal'
defineUnit('fortnight', {
duration: { weeks: 2 },
validate: (adapter, date) => true
})
const fortnight = usePeriod(temporal, 'fortnight')