Skip to content
Advertisement

How to get date range from week number with Luxon

I’m searching a way to get date range from week number with Luxon to replace my ‘moment’ code. Today I’m using this code:

JavaScript

I found a way to do that from a month number with ‘DateTime.fromObject()’ but that’s doesn’t work with ‘week’. So I don’t find the best way to do that from a week number 🙁

Thank’s in advance.

Advertisement

Answer

You can use DateTime.fromObject that:

Create a DateTime from a JavaScript object with keys like ‘year’ and ‘hour’ with reasonable defaults.

passing weekYear (an ISO week year) and weekNumber (an ISO week number, between 1 and 52 or 53, depending on the year) in the input object.

Here an live example:

JavaScript
JavaScript

You can also use DateTime.fromISO passing ISO compliant format such as YYYY-Www (see more on here). Please note that week number should be two digits.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement