Comprehensive Guide to Luxon DateTime Library for JavaScript Developers

Comprehensive Guide to Luxon DateTime Library for JavaScript Developers

Luxon is a powerful and modern library for working with dates and times in JavaScript. Developed by the creators of Moment.js, Luxon provides a rich API that is easy to use and comprehensive. In this guide, we will introduce Luxon’s core concepts and explore its dozen of useful APIs with code snippets. We’ll also showcase an app example that utilizes these APIs.

Getting Started

  
    // Importing Luxon
    const { DateTime } = require('luxon');

    // Alternatively, if using ES6 modules
    import { DateTime } from 'luxon';
  

Creating DateTime Instances

  
    // Current date and time
    let now = DateTime.local();

    // Specific date and time
    let dateTime = DateTime.local(2023, 10, 24, 9, 45);

    // From ISO string
    let isoDate = DateTime.fromISO('2023-10-24T09:45:00');

    // From SQL string
    let sqlDate = DateTime.fromSQL('2023-10-24 09:45:00');

    // From JavaScript Date object
    let jsDate = DateTime.fromJSDate(new Date());
  

Formatting DateTimes

  
    let dt = DateTime.local(2023, 10, 24, 9, 45);

    // Basic formatting
    console.log(dt.toString()); // 2023-10-24T09:45:00.000-04:00

    // Custom formatting
    console.log(dt.toFormat('yyyy LLL dd, HH:mm')); // 2023 Oct 24, 09:45

    // To ISO formats
    console.log(dt.toISO()); // 2023-10-24T09:45:00.000-04:00
    console.log(dt.toISODate()); // 2023-10-24
    console.log(dt.toISOTime()); // 09:45:00.000-04:00
  

Manipulating DateTimes

  
    let dt = DateTime.local();

    // Adding time
    let plusOneWeek = dt.plus({ weeks: 1 });

    // Subtracting time
    let minusTwoDays = dt.minus({ days: 2 });

    // Setting specific units
    let setHours = dt.set({ hour: 8, minute: 30 });
  

Working with Durations

  
    let duration = dt.diff(DateTime.local(2023, 10, 24, 9, 45), ['days', 'hours']);

    console.log(duration.toObject()); // { days: 0, hours: 0 }

    // Formatting durations
    console.log(duration.toFormat('d HH:mm:ss')); // 0 00:00:00
  

Example Application

Let’s build a simple reminder app that uses Luxon to set and display reminders.

  
    const { DateTime } = require('luxon');

    class ReminderApp {
      constructor() {
        this.reminders = [];
      }

      addReminder(note, dateTime) {
        let reminder = {
          note,
          dateTime: DateTime.fromISO(dateTime)
        };
        this.reminders.push(reminder);
        console.log(`Reminder added: ${note} at ${reminder.dateTime.toLocaleString(DateTime.DATETIME_MED)}`);
      }

      showReminders() {
        this.reminders.forEach(r => {
          console.log(`${r.note}: ${r.dateTime.toRelative()}`);
        });
      }
    }

    let app = new ReminderApp();
    app.addReminder('Doctor Appointment', '2023-10-25T09:30:00');
    app.addReminder('Team Meeting', '2023-10-26T14:00:00');
    app.showReminders();
  

In this simple reminder app, we demonstrate how to create reminders and display them relative to the current time.

Hash: 064cfb74420d2d573b05e14ad58792dd35c2504eb652bf069621854d4f7f2ba6

Leave a Reply

Your email address will not be published. Required fields are marked *