Skip to content

Date Adapters

Comprehensive Documentation

This page provides a quick overview. For detailed adapter comparisons, selection guides, and migration help, see our comprehensive adapter documentation.

useTemporal supports multiple date libraries through a simple adapter pattern.

Available Adapters

Native JavaScript Date (Default)

Zero dependencies, works everywhere:

typescript
import { createTemporal } from '@allystudio/usetemporal'

const temporal = createTemporal()
// Native adapter is included by default

Using date-fns

For advanced date operations:

bash
npm install @allystudio/usetemporal date-fns
typescript
import { createTemporal } from '@allystudio/usetemporal'
import { createDateFnsAdapter } from '@allystudio/usetemporal/date-fns'
import { enUS } from 'date-fns/locale'

const temporal = createTemporal({
  adapter: createDateFnsAdapter({ 
    locale: enUS,
    weekStartsOn: 1
  })
})

Using Luxon

For timezone support:

bash
npm install @allystudio/usetemporal luxon
typescript
import { createTemporal } from '@allystudio/usetemporal'
import { createLuxonAdapter } from '@allystudio/usetemporal/luxon'

const temporal = createTemporal({
  adapter: createLuxonAdapter({
    zone: 'America/New_York',
    locale: 'en-US'
  })
})

Using Temporal API

Future-proof with the upcoming Temporal API:

bash
npm install @allystudio/usetemporal @js-temporal/polyfill
typescript
import { createTemporal } from '@allystudio/usetemporal'
import { createTemporalAdapter } from '@allystudio/usetemporal/temporal'

const temporal = createTemporal({
  adapter: createTemporalAdapter()
})

Choosing an Adapter

AdapterBundle SizeFeaturesUse When
Native0KBBasic date operationsYou want minimal bundle size
date-fns~20KBExtensive date utilitiesYou need advanced formatting/parsing
Luxon~70KBTimezone support, i18nYou need timezone handling
Temporal~40KBModern API, immutableYou want future-proof code

Creating Custom Adapters

Implement the minimal adapter interface:

typescript
interface Adapter {
  startOf(date: Date, unit: Unit): Date
  endOf(date: Date, unit: Unit): Date
  add(date: Date, value: number, unit: Unit): Date
  diff(start: Date, end: Date, unit: Unit): number
}

Example custom adapter:

typescript
const myAdapter = {
  startOf(date, unit) {
    // Your implementation
  },
  endOf(date, unit) {
    // Your implementation
  },
  add(date, value, unit) {
    // Your implementation
  },
  diff(start, end, unit) {
    // Your implementation
  }
}

const temporal = createTemporal({ adapter: myAdapter })

Next Steps

Released under the MIT License.