Search code examples
luxon

Luxon invalid date


I'm using Luxon 3.3.0 and trying to parse this date.

const dateStr = '09-MAY-23 01.53.41.500211 PM';

const dateTime = DateTime.fromFormat(dateStr, 'dd-MMM-yy hh.mm.ss.SSSSSS a');

console.log(dateTime );

But I get an invalid date. Any ideas to why that would happen?

Here is the jsfiddle. https://jsfiddle.net/bnv7uw3t/2/


Solution

  • Problem in your code is that SSSSSS is not a valid format specifier.

    Upon closer inspection of the sources while DateTime objects only support up to milliseconds it is possible to parse your input with u specifier which, contrary to the docs accepts up to 9 digits (discarding part after first 3).

    const dateStr = '09-MAY-23 01.53.41.503211 PM';
    const dateTime = DateTime.fromFormat(dateStr, 'dd-MMM-yy hh.mm.ss.u a');
    console.log(dateTime);
    
    (...snip...)
     c: {
        day: 9,
        hour: 13,
        millisecond: 503,
        minute: 53,
        month: 5,
        second: 41,
        year: 2023
      },
    (...snip...)