Installation | | |
Install the Luxon library | npm install luxon | Luxon installed |
Import | | |
Import Luxon into your project | const { DateTime, Duration, Interval, Info, Settings } = require('luxon'); | Luxon imported |
DateTime Creation | | |
Create a DateTime object for the current date and time | const now = DateTime.now(); | 2023-04-08T10:30:00.000-05:00 (example) |
Create a DateTime object from specific date and time | const dt = DateTime.local(2023, 4, 8, 10, 30); | 2023-04-08T10:30:00.000 |
Create a DateTime object from an ISO 8601 string | const dt = DateTime.fromISO("2023-04-08T10:30:00"); | 2023-04-08T10:30:00.000 |
Create a DateTime object from milliseconds since epoch | const dt = DateTime.fromMillis(1680564300000); | 2023-04-08T10:30:00.000 |
Create a DateTime object from a JavaScript Date object | const dt = DateTime.fromJSDate(new Date()); | 2023-04-08T10:30:00.000 (example) |
DateTime Manipulation | | |
Add or subtract time | const added = dt.plus({ days: 7, hours: 2 }); | 2023-04-15T12:30:00.000 |
| const subtracted = dt.minus({ days: 5, hours: 1 }); | 2023-04-03T09:30:00.000 |
Set specific values | const setDateTime = dt.set({ month: 9, day: 1 }); | 2023-09-01T10:30:00.000 |
Start and end of specific unit | const startOfDay = dt.startOf('day'); | 2023-04-08T00:00:00.000 |
| const endOfMonth = dt.endOf('month'); | 2023-04-30T23:59:59.999 |
DateTime Formatting | | |
Format DateTime to a string | const formatted = dt.toFormat('yyyy-MM-dd HH:mm'); | "2023-04-08 10:30" |
Get an ISO 8601 string | const isoString = dt.toISO(); | "2023-04-08T10:30:00.000" |
Get a JavaScript Date object | const jsDate = dt.toJSDate(); | Sun Apr 08 2023 10:30:00 GMT-0500 (example) |
Time Zones | | |
Set a specific time zone | const zonedDt = dt.setZone('America/New_York'); | 2023-04-08T10:30:00.000-04:00 |
Get the local time zone | const localZone = DateTime.local().zoneName; | "America/New_York" (example) |
Convert to another time zone | const convertedDt = dt.setZone('Europe/London'); | 2023-04-08T15:30:00.000+01:00 |
Duration | | |
Create a duration object | const dur = Duration.fromObject({ hours: 1, minutes: 30 }); | Duration { hours: 1, minutes: 30 } |
Add a duration to a DateTime object | const newDt = dt.plus(dur); | 2023-04-08T12:00:00.000 |
Get the difference between two DateTime objects | const diff = dt1.diff(dt2); | Duration { hours: -1, minutes: -30 } (example) |
Format duration as a string | const formattedDur = dur.toFormat('hh:mm'); | "01:30" |
Interval | | |
Create an interval object | const interval = Interval.fromDateTimes(dt1, dt2); | Interval { start: dt1, end: dt2 } |
Check if a DateTime is within an interval | const contains = interval.contains(dt); | true or false |
Split interval into smaller intervals | const intervals = interval.splitBy(Duration.fromObject({ days: 1 })); | [Interval { ... }, Interval { ... }] (example) |
Info | | |
Get a list of available months | const months = Info.months('long'); | ["January", "February", ..., "December"] |
Get a list of available weekdays | const weekdays = Info.weekdays('long'); | ["Monday", "Tuesday", ..., "Sunday"] |
Get a list of available time zones | const timeZones = Info.timeZones(); | ["Africa/Abidjan", "Africa/Accra", ...] |