Skip to main content

Luxon

DescriptionExampleResult
Installation
Install the Luxon librarynpm install luxonLuxon installed
Import
Import Luxon into your projectconst { DateTime, Duration, Interval, Info, Settings } = require('luxon');Luxon imported
DateTime Creation
Create a DateTime object for the current date and timeconst now = DateTime.now();2023-04-08T10:30:00.000-05:00 (example)
Create a DateTime object from specific date and timeconst dt = DateTime.local(2023, 4, 8, 10, 30);2023-04-08T10:30:00.000
Create a DateTime object from an ISO 8601 stringconst dt = DateTime.fromISO("2023-04-08T10:30:00");2023-04-08T10:30:00.000
Create a DateTime object from milliseconds since epochconst dt = DateTime.fromMillis(1680564300000);2023-04-08T10:30:00.000
Create a DateTime object from a JavaScript Date objectconst dt = DateTime.fromJSDate(new Date());2023-04-08T10:30:00.000 (example)
DateTime Manipulation
Add or subtract timeconst 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 valuesconst setDateTime = dt.set({ month: 9, day: 1 });2023-09-01T10:30:00.000
Start and end of specific unitconst 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 stringconst formatted = dt.toFormat('yyyy-MM-dd HH:mm');"2023-04-08 10:30"
Get an ISO 8601 stringconst isoString = dt.toISO();"2023-04-08T10:30:00.000"
Get a JavaScript Date objectconst jsDate = dt.toJSDate();Sun Apr 08 2023 10:30:00 GMT-0500 (example)
Time Zones
Set a specific time zoneconst zonedDt = dt.setZone('America/New_York');2023-04-08T10:30:00.000-04:00
Get the local time zoneconst localZone = DateTime.local().zoneName;"America/New_York" (example)
Convert to another time zoneconst convertedDt = dt.setZone('Europe/London');2023-04-08T15:30:00.000+01:00
Duration
Create a duration objectconst dur = Duration.fromObject({ hours: 1, minutes: 30 });Duration { hours: 1, minutes: 30 }
Add a duration to a DateTime objectconst newDt = dt.plus(dur);2023-04-08T12:00:00.000
Get the difference between two DateTime objectsconst diff = dt1.diff(dt2);Duration { hours: -1, minutes: -30 } (example)
Format duration as a stringconst formattedDur = dur.toFormat('hh:mm');"01:30"
Interval
Create an interval objectconst interval = Interval.fromDateTimes(dt1, dt2);Interval { start: dt1, end: dt2 }
Check if a DateTime is within an intervalconst contains = interval.contains(dt);true or false
Split interval into smaller intervalsconst intervals = interval.splitBy(Duration.fromObject({ days: 1 }));[Interval { ... }, Interval { ... }] (example)
Info
Get a list of available monthsconst months = Info.months('long');["January", "February", ..., "December"]
Get a list of available weekdaysconst weekdays = Info.weekdays('long');["Monday", "Tuesday", ..., "Sunday"]
Get a list of available time zonesconst timeZones = Info.timeZones();["Africa/Abidjan", "Africa/Accra", ...]