Skip to content

stacksjs/ts-datetime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Social Card of ts-datetime

npm version GitHub Actions Commitizen friendly

ts-datetime

A modern, immutable, and fully-typed TypeScript datetime library inspired by Carbon (PHP) and Day.js.

Features

  • ๐Ÿ”„ 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

Installation

# npm
npm install @stacksjs/ts-datetime

# pnpm
pnpm add @stacksjs/ts-datetime

# yarn
yarn add @stacksjs/ts-datetime

# bun
bun add @stacksjs/ts-datetime

Quick Examples

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

Documentation

For full documentation, visit ts-datetime.netlify.app.

Feature Documentation

Advanced Topics

Testing

bun test

Changelog

Please see our releases page for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

Discussions on GitHub

For casual chit-chat with others using this package:

Join the Stacks Discord Server

Postcardware

โ€œ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 ๐ŸŒŽ

Credits

  • Carbon - The inspiration for this library.
  • Day.js - A lightweight alternative to Moment.js.

Sponsors

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.

License

The MIT License (MIT). Please see LICENSE for more information.

Made with ๐Ÿ’™

About

Modern datetime tooling. A Carbon-like API for TypeScript.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

  •