ReactJS - 如何更改输入字段的日期格式?

ReactJS - How to change the date format from an input field?

在 React 应用程序中,我使用的是一个数组,

const result = [
  {firstname: "Shruthi", lastname: "R", phone: "1234567890", birthday: "24/05/1991"},
  {firstname: "Maanya", lastname: "C", phone: "8882334324", birthday: "19/07/1990"},
  {firstname: "Chethan", lastname: "Kumar", phone: "7888382222", birthday: "24/05/1995"}
];

日期输入字段是,

<input type="date" onChange={props.handleOnChange} />

handleOnChange = (e) => {
  const date = e.target.value; // getting different format here
  console.log(date);
}

基于上述,数组列表 (result) 中的日期输入字段和生日字段包含 dd/mm/yyyy 格式,但在 handleOnChange 函数中,我在 [=15] 中获取它=]格式。

如何将日期格式更改为 dd/mm/yyyy

有一个名为 Intl.DateTimeFormat 的对象可以在 JavaScript 中启用语言敏感的日期和时间格式。您可以按如下方式使用它并进行所需的更改

console.log(new Intl.DateTimeFormat('en-US').format(date));

reference

你可以用这个方法

let today  = new Date("2020-11-18");
today.toLocaleDateString("en-US"); // 11/18/2020
today.toLocaleDateString("en-GB"); // 18/11/2020
today.toLocaleDateString("bn-IN"); // ১৮/১১/২০২০

或者你可以使用 moment

moment(moment('2020-11-18', 'YYYY-MM-DD')).format('DD-MM-YYYY');