【问题标题】:Difference between date-fns' isValid and luxon's isValiddate-fns 的 isValid 和 luxon 的 isValid 的区别
【发布时间】:2023-03-08 18:40:02
【问题描述】:

正如我的问题中提到的那样。我真的不明白 date-fns' isValid 和 luxon's isValid 之间的区别。我有一个日期字符串变量和一个变量,我用new Date() 调用日期字符串。

我的日期字符串是这样的:Fri Feb 11 2022 02:00:00 GMT+0200 (Eastern European Standard Time)

我有几个问题:

  1. 为什么日期字符串在date-fnsluxon 都无效?

  2. date-fns 中使用new Date() 调用的日期字符串变量返回false,但在luxon 中返回true

我在codesand-box分享了我的代码

这是我的全部代码

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>

`;

【问题讨论】:

    标签: date-fns luxon


    【解决方案1】:

    DateTime 的fromISO 接受字符串。我正在将字符串与Date 进行比较。那是错误。解决办法是fromJSDate

    DateTime.fromJSDate(dateString).isValid
    

    【讨论】:

      猜你喜欢
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多