luxon fails to parse this timestamp which moment can?

Created on 19 Nov 2017  路  6Comments  路  Source: moment/luxon

Hi, great work with Luxon, i tried playing with it today
I'm not sure if i'm doing something wrong here but..

const moment = require('moment');
let mm = moment('2017-11-18 12:47:20.684054+00:00');
console.log(mm);

Works fine, however

const DateTime = require('luxon').DateTime;
let dt = DateTime.fromString('2017-11-18 12:47:20.684054+00:00');
console.log(dt)

Fails with:

/Users/willij87/workspace/ella2/node_modules/luxon/build/cjs/luxon.js:3052
      for (var i = 0; i < fmt.length; i++) {
                             ^

TypeError: Cannot read property 'length' of undefined

Is this a known issue? Im not sure which format that timestamp is in, but luxon doesn't seem happy with it, fromISO didn't give much better results either :/

Most helpful comment

In case anyone ends up here: Luxon now has fromSQL()

All 6 comments

A few things going on here:

  1. Luxon's DateTime.fromString() takes two arguments, a string and a format. The error is really an arity error in disguise. I should make it throw something more helpful
  2. It's weird that Moment accepts that. It's accepting it as an ISO 8601 string, though it isn't (the space between the date and the time should be a "T"). Luxon's fromISO won't accept it without the T because AFAIK, it's not really an ISO format.
  3. I just merged #69 , which fixes fromISO's handling of additional decimal places, which you're also using.

So: grab the latest Luxon (0.0.20 is hot off the presses), replace your space with a T, and use fromISO:

DateTime.fromISO('2017-11-18 12:47:20.684054+00:00'.replace(' ', 'T'))

I'll work on the error message for fromString.

Amazing response, thank you very much.
If you're interested, that timestamp comes from Postgresql 9.5, its the "timestamp with time zone" type
https://www.postgresql.org/docs/9.5/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT

@icambron is there a seperate standard for SQL dates?
I tried to find if they follow a standard but couldn't come up with anything, https://www.postgresql.org/docs/9.5/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT

Ah, right, SQL dates.

https://www.ibm.com/support/knowledgecenter/en/SSGU8G_12.1.0/com.ibm.esqlc.doc/ids_esqlc_0190.htm
I'd be willing to add support to this to Luxon. I'll write up a ticket.

Great!
I can imagine a lot of people will be putting SQL timestamps straight into luxon, so it would be useful

In case anyone ends up here: Luxon now has fromSQL()

Was this page helpful?
0 / 5 - 0 ratings