【问题标题】:dateFormat not working -> TypeError: Invalid date [closed]dateFormat 不起作用-> TypeError:无效日期 [关闭]
【发布时间】:2022-01-18 13:32:24
【问题描述】:

我正在尝试将 2022-01-17T21:36:04.000Z 转换为 1 月 18 日类型格式。我在 npm package dateFormat 的帮助下做。

我可以通过以下方式轻松实现:

const date = dateFormat("2022-01-17T21:36:04.000Z", "mmmm dS");

但是,如果我像这样传递值,而不是硬编码,它似乎不起作用。

 
const convertDate = (dateformat) => {
  

    const date = dateFormat(`"${dateformat}"`, "mmmm dS");
    console.log(date);

我收到 TypeError: Invalid date。


console.log(`"${dateformat}"`)  // is giving me -> "2022-01-17T21:36:04.000Z"

【问题讨论】:

  • `"${dateformat}"` -> `${dateformat}`
  • 或者只是 dateformat 不带引号。

标签: javascript reactjs date


【解决方案1】:

代替

const date = dateFormat(`"${dateformat}"`, "mmmm dS");

直接使用变量:

const date = dateFormat(dateformat, "mmmm dS");

【讨论】:

    猜你喜欢
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2012-09-05
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多