Luxon note
Published: 2022-01-07 18:35:01

Quoted from https://moment.github.io/luxon/#/ and [API Docs](https://moment.github.io/luxon/api-docs/index.html)

# Installation
```shell
yarn add luxon
```
For TypeScript
```shell
yarn add -D @types/luxon
```

### ES6
```javascript
import {DateTime} from 'luxon';
```

### Node.js
```javascript
const {DateTime} = require('luxon');
```

# Creating a DateTime
### Current date and time in the local time zone
```javascript
const now = DateTime.local();
```

```javascript
const now = DateTime.now();
```

### Create from in the local time zone
May 30, 2021 at 3:30 PM in the local time zone:
```javascript
const dt = DateTime.local(2021, 5, 30, 15, 30)
```

### Create from an object
```javascript
const dt = DateTime.fromObject({
  day: 22,
  hour: 12,
  zone: 'America/Los_Angeles',
  numberingSystem: 'beng'
});
```

### Create from parsing ISO 8601
```javascript
const dt = DateTime.fromISO('2021-05-30');
```

# Parsing [[Doc](https://moment.github.io/luxon/#/parsing)]
### ISO 8601
```javascript
DateTime.fromISO("2016-05-25");
```

### Unix timestamps
```javascript
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](https://moment.github.io/luxon/#/formatting)]
### ISO 8601
```javascript
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
```javascript
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
```javascript
DateTime.fromISO('2014-08-06T13:07:04.054').toFormat('yyyy LLL dd'); //=> '2014 Aug 06'
```

#### Escape
```javascript
DateTime.now().toFormat("HH 'hours and' mm 'minutes'"); //=> '20 hours and 55 minutes'
```

#### Table of tokens
[Here](https://moment.github.io/luxon/#/formatting?id=table-of-tokens)

#### Getting at components [[Doc](https://moment.github.io/luxon/#/tour?id=getting-at-components)]
```javascript
dt = DateTime.now();
dt.year     //=> 2017
dt.month    //=> 9
dt.day      //=> 14
dt.second   //=> 47
dt.weekday  //=> 4
```

# Math [[Doc](https://moment.github.io/luxon/#/math)]
### Basics
```js
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 `>=`.
```js
d1 < d2 // is d1 before d2?
```

```js
d1.toMillis() === d2.toMillis() // are d1 and d2 the same instant in time?
+d1 === +d2 // same test, using object coercion
```

```js
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
```js
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'
```

```js
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 }
```

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