DEV Community

Ryu Yamashita
Ryu Yamashita

Posted on

Luxon note

Quoted from https://moment.github.io/luxon/#/ and API Docs

Installation

yarn add luxon
Enter fullscreen mode Exit fullscreen mode

For TypeScript

yarn add -D @types/luxon
Enter fullscreen mode Exit fullscreen mode

ES6

import {DateTime} from 'luxon';
Enter fullscreen mode Exit fullscreen mode

Node.js

const {DateTime} = require('luxon');
Enter fullscreen mode Exit fullscreen mode

Creating a DateTime

Current date and time in the local time zone

const now = DateTime.local();
Enter fullscreen mode Exit fullscreen mode
const now = DateTime.now();
Enter fullscreen mode Exit fullscreen mode

Create from in the local time zone

May 30, 2021 at 3:30 PM in the local time zone:

const dt = DateTime.local(2021, 5, 30, 15, 30)
Enter fullscreen mode Exit fullscreen mode

Create from an object

const dt = DateTime.fromObject({
  day: 22,
  hour: 12,
  zone: 'America/Los_Angeles',
  numberingSystem: 'beng'
});
Enter fullscreen mode Exit fullscreen mode

Create from parsing ISO 8601

const dt = DateTime.fromISO('2021-05-30');
Enter fullscreen mode Exit fullscreen mode

Parsing [Doc]

ISO 8601

DateTime.fromISO("2016-05-25");
Enter fullscreen mode Exit fullscreen mode

Unix timestamps

DateTime.fromMillis(1542674993410);
DateTime.fromSeconds(1542674993);
Enter fullscreen mode Exit fullscreen mode

JS Date Object

A native JS Date object can be converted into a DateTime using DateTime.fromJSDate.
An optional zone parameter can be provided to set the zone on the resulting object.

Formatting [Doc]

ISO 8601

dt.toISO(); //=> '2017-04-20T11:32:00.000-04:00'
dt.toISODate(); //=> '2017-04-20'
dt.toISOWeekDate(); //=> '2017-W17-7'
dt.toISOTime(); //=> '11:32:00.000-04:00'
Enter fullscreen mode Exit fullscreen mode

Human-readable, internationalized strings

dt.toLocaleString(); //=> '4/20/2017'
dt.toLocaleString(DateTime.DATETIME_FULL); //=> 'April 20, 2017, 11:32 AM EDT'
dt.setLocale('fr').toLocaleString(DateTime.DATETIME_FULL); //=> '20 avril 2017 à 11:32 UTC−4'
Enter fullscreen mode Exit fullscreen mode

Formatting with tokens

toFormat

DateTime.fromISO('2014-08-06T13:07:04.054').toFormat('yyyy LLL dd'); //=> '2014 Aug 06'
Enter fullscreen mode Exit fullscreen mode

Escape

DateTime.now().toFormat("HH 'hours and' mm 'minutes'"); //=> '20 hours and 55 minutes'
Enter fullscreen mode Exit fullscreen mode

Table of tokens

Here

Getting at components [Doc]

dt = DateTime.now();
dt.year     //=> 2017
dt.month    //=> 9
dt.day      //=> 14
dt.second   //=> 47
dt.weekday  //=> 4
Enter fullscreen mode Exit fullscreen mode

Math [Doc]

Basics

DateTime.local(2017, 2, 13).plus({ months: 1 }).toISODate() //=> '2017-03-13'
DateTime.local(2017, 2, 13).plus({ days: 30 }).toISODate() //=> '2017-03-15'
DateTime.fromISO('2017-05-15').plus({months: 2, days: 6}).toISODate(); //=> '2017-07-21'
DateTime.fromISO('2017-04-30').plus({days: 1}).plus({months: 1}).toISODate() //=> '2017-06-01'
Enter fullscreen mode Exit fullscreen mode

Comparing DateTimes

We can compare DateTimes with <, >, <=, and >=.

d1 < d2 // is d1 before d2?
Enter fullscreen mode Exit fullscreen mode
d1.toMillis() === d2.toMillis() // are d1 and d2 the same instant in time?
+d1 === +d2 // same test, using object coercion
Enter fullscreen mode Exit fullscreen mode
const d1 = DateTime.fromISO('2017-04-30');
const d2 = DateTime.fromISO('2017-04-01');

d2 < d1                                   //=> true
d2.startOf('year') < d1.startOf('year')   //=> false
d2.startOf('month') < d1.startOf('month') //=> false
d2.startOf('day') < d1.startOf('day')     //=> true
Enter fullscreen mode Exit fullscreen mode

Duration/Diffs

const dur = Duration.fromObject({ days: 3, hours: 6})

// examine it
dur.toObject()          //=> { days: 3, hours: 6 }

// express in minutes
dur.as('minutes')       //=> 4680

// convert to minutes
dur.shiftTo('minutes').toObject() //=> { minutes: 4680 }

// add to a DateTime
DateTime.fromISO("2017-05-15").plus(dur).toISO() //=> '2017-05-18T06:00:00.000-04:00'
Enter fullscreen mode Exit fullscreen mode
const end = DateTime.fromISO('2017-03-13');
const start = DateTime.fromISO('2017-02-13');

const diff = end.diff(start);
diff.toObject() //=> { milliseconds: 2415600000 }

const diffInMonths = end.diff(start, 'months');
diffInMonths.toObject(); //=> { months: 1 }
Enter fullscreen mode Exit fullscreen mode
const end = DateTime.fromISO('2017-03-13');
const start = DateTime.fromISO('2017-02-11');
end.diff(start, ['months', 'days']).toObject() //=> { months: 1, days: 2 }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)