英文:
Conversion of Italian calendar for react-multi-date-picker library
问题
我在我的项目中使用了react-multi-date-picker
,并且我正在使用多种模式。因此,当我的语言是意大利语时,我必须使用意大利语日历。我尝试了locale="it"
,但它没有起作用。经过研究,我找到了一个解决方案,即创建一个包含意大利语月份、星期几和时段的JavaScript文件。此外,我还为react-date-object
提供了引用。在那里创建了各种类型的区域设置JavaScript文件。我创建了相同的文件并尝试将其传递给DatePicker,但它显示了错误的输出。
以下是我的App.js
文件用于DatePicker:
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;
我的italianCalendar
文件如下:
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
英文:
I have used react-multi-date-picker for my project and I am using multiple mode. For this I have to use Italian calendar when my language is Italian. I was tried with locale="it" but it is not working. After research, I got one solution which is, create js file which contain Italian months, weekdays numbers and meridiems. Also I had given reference for react-date-object. In that various types of the locale js files created. created same and tried to pass it to DatePicker but it showing me wrong output.
Actually, calendar is converting well but date is set to wrong days.
Here is the my App.js file for DatePicker
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-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"], ], };
<!-- end snippet -->
Output is showing like this.
For reference, I have added my code over sandbox
答案1
得分: 1
你可以使用 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";
// 使用意大利日历创建一个新的 DateObject
const italianDateObject = new DateObject({ calendar: "italian" });
function App() {
// 将默认日历设置为意大利日历
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"));
请注意,以上代码是使用 react-date-object 库创建一个支持意大利日历的日期选择器的示例。
英文:
You could use the react-date-object library. It provides a DateObject class which can handle different kind of calendars, try this :
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"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论