简体   繁体   English

如何在反应中更改日期格式?

[英]How to change date format in react?

I am unable to change the date format for which I am not able to show the date in frontend.我无法更改无法在前端显示日期的日期格式。

I am using dateformat package.我正在使用 dateformat 包。

import dateFormat from "dateformat";
const EditFinancialInfo = ({ info }) => {
  const [moneyToBePaid, setMoneyToBePaid] = useState({
    dueDate: dateFormat(info.moneyToBePaid.dueDate, "dd-MM-yyyy"),
  })

  return (
    <div>
      <input label="Due" type="date"
        name="dueDate"
        onChange={mtpHandleChange}
        value={moneyToBePaid.dueDate}
      />
    </div>
  )
}

I am getting this error我收到此错误

The specified value "15-30-2020" does not conform to the required format, "yyyy-MM-dd"

Default format for input type="date" is YYYY-MM-DD and it is impossible to change. input type="date"默认格式是YYYY-MM-DD并且无法更改。

You need to re-format your date value to match with input date format您需要重新格式化日期值以匹配输入日期格式

dueDate: dateFormat(info.moneyToBePaid.dueDate, "yyyy-MM-dd")

这里的问题是你日期的月份,月份必须是从 1 到 12,这意味着一月到十二月,但你的月份是 30,这就是出现这个错误的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM