Search code examples
date-fnsluxon

Difference between date-fns' isValid and luxon's isValid


As mentioned in my question. I really don't understand difference between date-fns' isValid and luxon's isValid. I have one date string variable and one variable which I invoke the date string with new Date().

My date string is like this: Fri Feb 11 2022 02:00:00 GMT+0200 (Eastern European Standard Time)

I have few questions:

  1. Why the date string is not valid both date-fns and luxon?

  2. The invoked date string variable with new Date() in date-fns it returns false but in luxon it return true

I shared my code in codesand-box

This is my all code

import { isValid } from "date-fns";
import { DateTime } from "luxon";

const onlyDateString =
  "Fri Feb 11 2022 :00:00 GMT+0200 (Eastern European Standard Time)";

const dateString = new Date(
  "Fri Feb 11 2022 :00:00 GMT+0200 (Eastern European Standard Time)"
);

const dateStringDateFns = isValid(onlyDateString);
const dateStringLuxon = DateTime.fromISO(onlyDateString).isValid;

const fs = isValid(dateString);
const lu = DateTime.fromISO(dateString).isValid;



document.getElementById("app").innerHTML = `
<p> Date string </p>
<div> ${dateStringDateFns}</div>
<div>${dateStringLuxon}</div>
<br/>
<p> Date string with new Date </p>
<div>${fs}</div>
<div>${lu}</div>

`;


Solution

  • DateTime's fromISO takes string. I was comparing string with Date. That was the mistake. Solution is fromJSDate

    DateTime.fromJSDate(dateString).isValid