skip to Main Content

I’m trying to define holidays in Luxon, such as memorial day which is the last Monday of May. How can I generate a luxon date object with this:

DateTime.now().set({ month: 5, weekday: 1 })

Is there a way to specify last week in Luxon without knowing the exact date?

2

Answers


  1. DateTime.now().set({ month: 5 }).endOf('month').startOf('week');
    

    From my understanding, Luxon’s weeks always start on Monday, so asking for the start of the week after getting the end of the month of May should get you the last Monday of May (of the current year if your starting point is DateTime.now()).

    Login or Signup to reply.
  2. You can, of course also do it without Luxon by using some basic Date functionality:

    function getLastMondayOfMay(y){
     const dd=new Date(y,4,31), wd=dd.getDay(); // set date to last day of May and get weekday
     dd.setDate(31-(wd+7-1)%7); // find the closest Monday before that
     return dd;
    }
    
    const options = {weekday: "long",year: "numeric",month: "long",
                    day: "numeric"};
    
    [2018,2019,2020,2021,2022,2023,2024,2025].forEach(y=>
      console.log(getLastMondayOfMay(y).toLocaleString('en-EN',options)))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search