Convert Figma logo to code with AI

moment logoluxon

⏱ A library for working with dates and times in JS

15,239
731
15,239
179

Top Related Projects

34,434

⏳ Modern JavaScript date utility library ⌛️

46,619

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

5,095

Tiny millisecond conversion utility

1,613

:clock2: Immutable date and time library for javascript

8,499

A positioning engine to make overlays, tooltips and dropdowns better

Quick Overview

Luxon is a powerful JavaScript library for working with dates and times. It provides a comprehensive set of features for parsing, formatting, manipulating, and performing calculations on dates and times, with a focus on immutability and clarity.

Pros

  • Immutable API, preventing unintended side effects
  • Comprehensive support for time zones and internationalization
  • Robust parsing and formatting capabilities
  • Well-documented and actively maintained

Cons

  • Larger bundle size compared to some alternatives
  • Steeper learning curve for developers new to date/time libraries
  • Not as widely adopted as some other date libraries (e.g., Moment.js)

Code Examples

Creating a DateTime object:

const { DateTime } = luxon;
const now = DateTime.now();
console.log(now.toString()); // Output: 2023-04-20T12:34:56.789-04:00

Formatting dates:

const date = DateTime.local(2023, 4, 20);
console.log(date.toFormat('yyyy LLL dd')); // Output: 2023 Apr 20

Performing date arithmetic:

const date = DateTime.local(2023, 4, 20);
const futureDate = date.plus({ days: 7, hours: 12 });
console.log(futureDate.toISO()); // Output: 2023-04-28T12:00:00.000-04:00

Working with time zones:

const local = DateTime.local();
const inTokyo = local.setZone('Asia/Tokyo');
console.log(inTokyo.toFormat('HH:mm ZZZZ')); // Output: 01:34 Japan Standard Time

Getting Started

To use Luxon in your project, first install it via npm:

npm install luxon

Then, import and use it in your JavaScript code:

import { DateTime } from 'luxon';

const now = DateTime.now();
console.log(now.toFormat('yyyy-MM-dd HH:mm:ss'));

For browser usage, you can include Luxon via a CDN:

<script src="https://cdn.jsdelivr.net/npm/luxon@3.3.0/build/global/luxon.min.js"></script>
<script>
  const { DateTime } = luxon;
  const now = DateTime.now();
  console.log(now.toFormat('yyyy-MM-dd HH:mm:ss'));
</script>

Competitor Comparisons

34,434

⏳ Modern JavaScript date utility library ⌛️

Pros of date-fns

  • Modular architecture allows for tree-shaking and smaller bundle sizes
  • Immutable by design, reducing side effects and improving predictability
  • Extensive localization support with 80+ locales available

Cons of date-fns

  • Less comprehensive API compared to Luxon
  • Lacks some advanced features like time zones and durations

Code Comparison

date-fns:

import { format, addDays } from 'date-fns'

const date = new Date()
const formattedDate = format(date, 'yyyy-MM-dd')
const futureDate = addDays(date, 7)

Luxon:

import { DateTime } from 'luxon'

const date = DateTime.now()
const formattedDate = date.toFormat('yyyy-MM-dd')
const futureDate = date.plus({ days: 7 })

Key Differences

  • date-fns uses separate functions for operations, while Luxon uses a more object-oriented approach
  • Luxon provides a richer API for complex operations and time zone handling
  • date-fns focuses on simplicity and modularity, making it easier to use only the required functions
  • Luxon offers better parsing capabilities and more flexible formatting options

Both libraries are modern alternatives to Moment.js, with date-fns being more lightweight and Luxon offering more features. The choice between them depends on the specific requirements of your project and personal preference.

46,619

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

Pros of Day.js

  • Extremely lightweight (2KB minified and gzipped)
  • API largely compatible with Moment.js, making migration easier
  • Immutable by default, reducing unexpected side effects

Cons of Day.js

  • Less comprehensive feature set compared to Luxon
  • Fewer built-in parsing options for complex date formats
  • Limited support for time zones without additional plugins

Code Comparison

Day.js:

import dayjs from 'dayjs'

const now = dayjs()
const formatted = now.format('YYYY-MM-DD')
const nextWeek = now.add(1, 'week')

Luxon:

import { DateTime } from 'luxon'

const now = DateTime.now()
const formatted = now.toFormat('yyyy-MM-dd')
const nextWeek = now.plus({ weeks: 1 })

Both libraries offer similar functionality for basic date operations, but Luxon provides more advanced features out of the box. Day.js focuses on simplicity and small size, while Luxon offers a more comprehensive set of tools for complex date and time manipulations. Day.js is an excellent choice for projects that require basic date handling and prioritize minimal bundle size, whereas Luxon is better suited for applications with more advanced date and time requirements, especially those dealing with time zones and complex parsing scenarios.

5,095

Tiny millisecond conversion utility

Pros of ms

  • Extremely lightweight and focused on a single task (parsing time strings)
  • Simple API with minimal learning curve
  • Tiny bundle size, ideal for projects where size is critical

Cons of ms

  • Limited functionality compared to Luxon's comprehensive date/time manipulation
  • Lacks support for complex date operations and formatting
  • No built-in internationalization or timezone handling

Code Comparison

ms:

const ms = require('ms');
console.log(ms('2 days'));  // 172800000
console.log(ms('1h'));      // 3600000
console.log(ms(60000));     // "1m"

Luxon:

const { DateTime } = require("luxon");
const dt = DateTime.now().plus({ days: 2 });
console.log(dt.toRelative());  // "in 2 days"
console.log(dt.toFormat("yyyy-MM-dd"));  // "2023-05-15"

Summary

ms is a minimalist library for parsing simple time strings, while Luxon offers comprehensive date and time manipulation. ms excels in simplicity and small size, making it ideal for projects with basic time parsing needs. Luxon, on the other hand, provides a rich set of features for complex date operations, formatting, and internationalization, making it more suitable for applications requiring extensive date/time handling.

1,613

:clock2: Immutable date and time library for javascript

Pros of js-joda

  • Immutable API, preventing unintended side effects
  • Closer alignment with Java 8's java.time API, beneficial for developers familiar with Java
  • Supports a wider range of calendar systems (e.g., Japanese, Minguo, Thai Buddhist)

Cons of js-joda

  • Smaller community and ecosystem compared to Luxon
  • Less comprehensive documentation and fewer examples available
  • Steeper learning curve for developers not familiar with Java's time API

Code Comparison

js-joda:

const LocalDate = require('@js-joda/core').LocalDate;
const date = LocalDate.parse('2023-05-15');
const newDate = date.plusDays(7);
console.log(newDate.toString()); // 2023-05-22

Luxon:

const { DateTime } = require('luxon');
const date = DateTime.fromISO('2023-05-15');
const newDate = date.plus({ days: 7 });
console.log(newDate.toISODate()); // 2023-05-22

Summary

Both js-joda and Luxon are modern date-time libraries for JavaScript, offering improvements over older options like Moment.js. js-joda provides a more Java-like experience and supports additional calendar systems, while Luxon offers a larger ecosystem and more approachable API for JavaScript developers. The choice between them often depends on the project's specific requirements and the team's familiarity with Java's time API.

8,499

A positioning engine to make overlays, tooltips and dropdowns better

Pros of Tether

  • Lightweight and focused on positioning elements
  • Simple API for attaching elements to other elements or fixed positions
  • Works well for creating tooltips, popovers, and dropdown menus

Cons of Tether

  • Limited functionality compared to Luxon's date/time manipulation capabilities
  • Not designed for handling date and time operations
  • Lacks internationalization support

Code Comparison

Tether (positioning elements):

new Tether({
  element: '#tooltip',
  target: '#target-element',
  attachment: 'top center'
});

Luxon (date/time manipulation):

const DateTime = luxon.DateTime;
const now = DateTime.now();
const formatted = now.toFormat('yyyy-MM-dd HH:mm:ss');

Summary

Tether and Luxon serve different purposes. Tether is a positioning library for attaching elements to other elements or fixed positions, making it useful for creating UI components like tooltips. Luxon, on the other hand, is a powerful date and time manipulation library with extensive features for parsing, formatting, and working with dates and times.

While Tether excels in its specific use case of element positioning, it lacks the broader functionality offered by Luxon for date and time operations. Luxon provides a more comprehensive solution for applications that require advanced date and time handling, including internationalization support.

Choose Tether for projects focused on UI element positioning, and Luxon for applications that need robust date and time manipulation capabilities.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Luxon

MIT License Build Status NPM version Coverage Status PRs welcome

Luxon is a library for working with dates and times in JavaScript.

DateTime.now().setZone("America/New_York").minus({ weeks: 1 }).endOf("day").toISO();

Upgrading to 3.0

Guide

Features

  • DateTime, Duration, and Interval types.
  • Immutable, chainable, unambiguous API.
  • Parsing and formatting for common and custom formats.
  • Native time zone and Intl support (no locale or tz files).

Download/install

Download/install instructions

Documentation

Development

See contributing.

Phasers to stun

NPM DownloadsLast 30 Days