'Luxonjs and mongoose

I'm trying to migrate from momentjs to luxonjs and I'm facing an issue regarding the date format sent by mongoose.

In mongoose docs, date format is a RFC822. Exemple : Fri Jun 25 2021 08:36:03 GMT+0200 (heure d’été d’Europe centrale)

When using moment, just doing a moment(mongooseDate) works perfectly.

I tried many different functions on luxon without success, hHow to make luxon work with mongoose datetime result.

Thanks

Code Exemple

//date is the result of the query on a date field
  console.log('MOONGOOSE DATE :', date)
  const momentValue = moment(date)
  console.log('MOMENT : ', momentValue)
  const luxonValue = DateTime.fromRFC2822(date)
  console.log('LUXON : ', luxonValue.toObject())

Code Exemple

Console Result



Solution 1:[1]

I was having same problem with luxon, so you need to try converting .fromISO, instead of .fromRFC2822

  console.log('MOONGOOSE DATE :', date)
  const momentValue = moment(date)
  console.log('MOMENT : ', momentValue)
  const luxonValue = DateTime.fromISO(date)
  console.log('LUXON : ', luxonValue.toObject())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1