Quoted from https://moment.github.io/luxon/#/ and API Docs
Installation
yarn add luxon
For TypeScript
yarn add -D @types/luxon
ES6
import {DateTime} from 'luxon';
Node.js
const {DateTime} = require('luxon');
Creating a DateTime
Current date and time in the local time zone
const now = DateTime.local();
const now = DateTime.now();
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)
Create from an object
const dt = DateTime.fromObject({
day: 22,
hour: 12,
zone: 'America/Los_Angeles',
numberingSystem: 'beng'
});
Create from parsing ISO 8601
const dt = DateTime.fromISO('2021-05-30');
Parsing [Doc]
ISO 8601
DateTime.fromISO("2016-05-25");
Unix timestamps
DateTime.fromMillis(1542674993410);
DateTime.fromSeconds(1542674993);
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'
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'
Formatting with tokens
toFormat
DateTime.fromISO('2014-08-06T13:07:04.054').toFormat('yyyy LLL dd'); //=> '2014 Aug 06'
Escape
DateTime.now().toFormat("HH 'hours and' mm 'minutes'"); //=> '20 hours and 55 minutes'
Table of tokens
Getting at components [Doc]
dt = DateTime.now();
dt.year //=> 2017
dt.month //=> 9
dt.day //=> 14
dt.second //=> 47
dt.weekday //=> 4
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'
Comparing DateTimes
We can compare DateTimes with <
, >
, <=
, and >=
.
d1 < d2 // is d1 before d2?
d1.toMillis() === d2.toMillis() // are d1 and d2 the same instant in time?
+d1 === +d2 // same test, using object coercion
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
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'
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 }
const end = DateTime.fromISO('2017-03-13');
const start = DateTime.fromISO('2017-02-11');
end.diff(start, ['months', 'days']).toObject() //=> { months: 1, days: 2 }
Top comments (0)