Search code examples
javascriptluxon

Truncate date string into just year and week


I'm using Luxon to manipulate Dates in my code. My method returns a date like this:

DateTime.utc(1982, 5, 25).toISOWeekDate() //=> '1982-W21-2'

I need to knock off the day part and have '1982-W21'instead.

Not too familiar with js, how would I "knock off" the day part..


Solution

  • You can split, slice and join the result, or extract a substring like this:

    console.log('1982-W21-2'.split('-').slice(0, 2).join("-"));
    
    // or
    const dateResult = "1982-W21-2'"
    console.log(dateResult.substr(0, dateResult.lastIndexOf("-")));