A modern, immutable, and fully-typed TypeScript datetime library inspired by Carbon (PHP) and Day.js.
- 🔄 Immutable API All operations return new instances
- ⛓️ Fluent Interface Chainable, readable, and expressive
- 🌍 Locale Support Global, per-instance, and per-call configuration
- 📅 Robust Parsing ISO, timestamps, relative strings (e.g. "next week", "+2 days")
- ⏱️ Intervals & Periods Built-in support for durations and date ranges
- 💪 TypeScript First Fully typed, with great DX in modern editors
- ✅ Tested Thoroughly tested for edge cases and correctness
# npm
npm install @stacksjs/ts-datetime
# pnpm
pnpm add @stacksjs/ts-datetime
# yarn
yarn add @stacksjs/ts-datetime
# bun
bun add @stacksjs/ts-datetime
import { Datetime, DatetimeInterval, DatetimePeriod } from 'ts-datetime'
// Creating dates
const now = Datetime.now()
const tomorrow = Datetime.tomorrow()
const specificDate = new Datetime('2024-01-01T12:00:00Z')
// Manipulation
const nextWeek = now.addDays(7)
const lastMonth = now.subMonths(1)
// Formatting
console.log(specificDate.format('YYYY-MM-DD')) // "2024-01-01"
console.log(now.diffForHumans()) // "just now"
// Intervals
const interval = DatetimeInterval.days(5).add(DatetimeInterval.hours(12))
console.log(interval.forHumans()) // "5 days 12 hours"
// Periods (date ranges)
const period = new DatetimePeriod(
new Datetime('2024-01-01'),
new Datetime('2024-01-10'),
DatetimeInterval.days(2)
)
// Iterate over period
for (const date of period) {
console.log(date.format('YYYY-MM-DD'))
}
// 2024-01-01, 2024-01-03, 2024-01-05, 2024-01-07, 2024-01-09
For full documentation, visit ts-datetime.netlify.app.
bun test
Please see our releases page for more information on what has changed recently.
Please see CONTRIBUTING for details.
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
“Software that is free, but hopes for a postcard.” We love receiving postcards from around the world showing where Stacks is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
The MIT License (MIT). Please see LICENSE for more information.
Made with 💙