Search code examples
javascriptluxon

luxon endOf month resolving wrong date


i'm using luxon for date calculation and try to get the end of a month in german timezone. I tried the following code, but it gave me '2022-08-30T23:59:59.999+02:00'. But august has 31 days.

DateTime.fromISO('2022-07-31T10:00:00')
            .setZone("Europe/Berlin")
            .plus({month: 1})
            .endOf('month')
            .toString() // '2022-08-30T23:59:59.999+02:00'

(https://codesandbox.io/s/luxon-playground-forked-srh81m)

What am I missing?


Solution

  • I think that issue showed in the linked CodeSandbox is due to old version of luxon used (1.2.0).

    You'll get the expected result using version >= 1.21.2, see changelog:

    1.21.2 (2019-11-18)

    • Fix bug in Chrome Canary that threw off time zone calculations

    Working example:

    const DateTime = luxon.DateTime;
    console.log(
    DateTime.fromISO('2022-07-31T10:00:00')
                .setZone("Europe/Berlin")
                .plus({month: 1})
                .endOf('month')
                .toString() 
    );
    <script src="https://cdn.jsdelivr.net/npm/luxon@1.21.2/build/global/luxon.min.js"></script>