Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Luxon invalid date

Tags:

luxon

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/

like image 273
Jason Foglia Avatar asked Oct 16 '25 02:10

Jason Foglia


1 Answers

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...)
like image 119
orhtej2 Avatar answered Oct 19 '25 13:10

orhtej2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!