reactjs 意大利历在React式多日期选择器库中的转换

qkf9rpyu  于 2023-03-01  发布在  React
关注(0)|答案(1)|浏览(130)

我已经为我的项目使用了react-multi-date-picker,我正在使用多个模式。为此,我必须使用意大利日历时,我的语言是意大利语。我尝试与locale=“it”,但它是行不通的。经过研究,我得到了一个解决方案,这是,创建包含意大利月份的js文件,工作日的数字和子午线。我也给出了react-date-object的参考。在那里创建了各种类型的locale js文件。创建了相同的,并试图将其传递给日期选择器,但它显示我错误的输出。实际上,日历转换良好,但日期设置为错误的日期。
下面是DatePicker的my App.js文件

import DatePicker from "react-multi-date-picker";
import DatePanel from "react-multi-date-picker/plugins/date_panel";
import italian_it from './italianCalendar';
function App() {
  return (
    <div className="App">
      <header className="App-header">
        <DatePicker
          locale={italian_it}
          multiple
          format="DD/MM/YYYY"
          plugins={[<DatePanel />]}
        />
      </header>
    </div>
  );
}

export default App;

My italianCalendar file is as below,

   

  module.exports = { name: "italian_it", months: [ ["gennaio", "gennaio"], ["febbraio", "febbraio"], ["marzo", "marzo"], ["aprile", "aprile"], ["maggio", "maggio"], ["giugno", "giugno"], ["luglio", "luglio"], ["agosto", "agosto"], ["settembre", "settembre"], ["ottobre", "ottobre"], ["novembre", "novembre"], ["dicembre", "dicembre"], ], weekDays: [ ["domenica", "dom"], ["lunedì", "lun"],  ["martedì", "mar"], ["mercoledì", "mer"], ["giovedì", "gio"], ["venerdì", "ven"], ["sabato", "sab"]], digits: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], meridiems: [ ["am", "am"], ["pm", "pm"], ],   };

输出如下所示。

作为参考,我在沙箱中添加了我的代码
https://codesandbox.io/s/keen-worker-mnvdno

eimct9ow

eimct9ow1#

你可以使用react-date-object库,它提供了一个DateObject类,可以处理不同类型的日历,试试这个:

import React from "react";
import ReactDOM from "react-dom";
import DatePicker from "react-multi-date-picker";
import DatePanel from "react-multi-date-picker/plugins/date_panel";
import { DateObject } from "react-date-object";

// Create a new DateObject with the Italian calendar
const italianDateObject = new DateObject({ calendar: "italian" });

function App() {
  // Set the default calendar to the Italian calendar
  const defaultCalendar = italianDateObject.calendar;

  return (
    <div className="App">
      <header className="App-header">
        <DatePicker
          defaultCalendar={defaultCalendar}
          multiple
          format="DD/MM/YYYY"
          plugins={[<DatePanel />]}
        />
      </header>
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));

相关问题