Javascript 用于分析不同对象数组中的日期的函数
我有一些数组定义如下:Javascript 用于分析不同对象数组中的日期的函数,javascript,reactjs,Javascript,Reactjs,我有一些数组定义如下: const ops = [ { value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OPS', date: '31.12' } ]; const ops1 = [ { value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OPS 1', date: '01.07', nextYear: 'true' } ]; const oms = [
const ops = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OPS', date: '31.12' }
];
const ops1 = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OPS 1', date: '01.07', nextYear: 'true' }
];
const oms = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OMS', date: '31.12' }
];
const periodDate = ops.find(item => item.type === "period").date;
const [day, month] = periodDate ? periodDate.split(".") : [0, 0];
const yearFull = '2019';
const duedate = new Date();
duedate.setDate(Number(day));
duedate.setMonth(Number(month) - 1);
duedate.setFullYear(parseInt(yearFull) + 1);
我需要通过函数getWorkingDay()
解析三个不同位置的截止日期。到期日期由数组对象中的日期
和年份
值组成,该值通过react final form
值传递。如果对象具有nextYear:“true”
,则年份应递增一
我可以获得单个阵列(例如,ops
)的到期日期,如下所示:
const ops = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OPS', date: '31.12' }
];
const ops1 = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OPS 1', date: '01.07', nextYear: 'true' }
];
const oms = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OMS', date: '31.12' }
];
const periodDate = ops.find(item => item.type === "period").date;
const [day, month] = periodDate ? periodDate.split(".") : [0, 0];
const yearFull = '2019';
const duedate = new Date();
duedate.setDate(Number(day));
duedate.setMonth(Number(month) - 1);
duedate.setFullYear(parseInt(yearFull) + 1);
然后通过getWorkingDay()
和format()
解析它:
{格式(新日期(getWorkingDay(duedate)),'dd.MM.yyyy')}
我的问题是:
如何创建一个用于所有数组的函数,在该函数中我可以将数组的名称作为参数传递
如何将year
值作为react final form
值中的第二个参数传递到此函数,而不是硬编码const yearFull='2019'代码>
我如何从该函数中检查nextYear:“true”
,以将年份增加一个?(duedate.setFullYear
应为年份或年份+1,此时nextYear
为true)
将date
作为对象或数组而不是字符串来使用此函数更好吗
你可以这样做。创建一个包含这三个数组的新对象,并使用一个方法通过传递要访问的数组的名称、年份(按格式)和日期的格式返回格式化的工作日
const ops = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OPS', date: '31.12' }
];
const ops1 = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OPS 1', date: '01.07', nextYear: 'true' }
];
const oms = [
{ value: 'YY.00', label: 'Year', type: 'period', info: 'Tax OMS', date: '31.12' }
];
const myObject = {
ops,
ops1,
oms
};
const formatedWorkingDate = (name, year, dateFormat) => {
//get the period object by accessing the array in myObject
const period = myObject[name].find(p => p.type === "period");
//get periodDate
const periodDate = period.date;
const [day, month] = periodDate ? periodDate.split(".") : [0, 0];
const duedate = new Date();
duedate.setDate(Number(day));
duedate.setMonth(Number(month) - 1);
//if nextYear set year + 1 otherwise set only year
duedate.setFullYear(period.nextYear ? parseInt(year) + 1 : parseInt(year));
//return formated date
return format(new Date(duedate), dateFormat);
};
return (
<Form
onSubmit={onSubmit}
render={({ handleSubmit, values }) => (
<form onSubmit={handleSubmit}>
call formatedWorkingDate function by passing the name of the array that
we need to access, year value from input form and format that we want
the date to be
<div>{formatedWorkingDate("ops", values.year, "dd.MM.yyyy")}</div>
<div>{formatedWorkingDate("ops1", values.year, "dd.MM.yyyy")}</div>
<div>{formatedWorkingDate("oms", values.year, "dd.MM.yyyy")}</div>
</form>
)}
/>
);
const ops=[
{值:'YY.00',标签:'Year',类型:'period',信息:'Tax OPS',日期:'31.12'}
];
常数ops1=[
{value:'YY.00',label:'Year',type:'period',info:'Tax OPS 1',date:'01.07',nextYear:'true'}
];
常数oms=[
{值:'YY.00',标签:'Year',类型:'period',信息:'Tax OMS',日期:'31.12'}
];
常量myObject={
行动计划,
ops1,
oms
};
const formatedWorkingDate=(名称、年份、日期格式)=>{
//通过访问myObject中的数组获取period对象
const period=myObject[name].find(p=>p.type==“period”);
//获取周期日期
const periodDate=period.date;
常量[日,月]=periodDate?periodDate.split(“.”:[0,0];
const duedate=新日期();
duedate.setDate(数字(天));
duedate.setMonth(编号(月份)-1);
//如果nextYear设置年份+1,则仅设置年份
duedate.setFullYear(period.nextYear?parseInt(year)+1:parseInt(year));
//返回格式化日期
返回格式(新日期(duedate)、日期格式);
};
返回(
(
通过传递所需数组的名称来调用FormattedWorkingDate函数
我们需要访问输入表单中的年值和我们想要的格式
日期
{格式化工作日期(“ops”,values.year,“dd.MM.yyyy”)}
{格式化工作日期(“ops1”,values.year,“dd.MM.yyyy”)}
{格式化工作日期(“oms”,values.year,“dd.MM.yyyy”)}
)}
/>
);
如果所有的格式都相同,则只需在方法内部调用该格式,而不传递:
const formatedWorkingDate = (name, year) => {
//other part of code
//return formated date
return format(new Date(duedate), "dd.MM.yyyy");
};
return (
<Form
onSubmit={onSubmit}
render={({ handleSubmit, values }) => (
<form onSubmit={handleSubmit}>
call formatedWorkingDate function by passing the name of the array that
we need to access, year value from input form
<div>{formatedWorkingDate("ops", values.year)}</div>
<div>{formatedWorkingDate("ops1", values.year)}</div>
<div>{formatedWorkingDate("oms", values.year)}</div>
</form>
)}
/>
);
const formattedworkingdate=(名称、年份)=>{
//代码的其他部分
//返回格式化日期
返回格式(新日期(截止日期),“dd.MM.yyyy”);
};
返回(
(
通过传递所需数组的名称来调用FormattedWorkingDate函数
我们需要从输入表单中访问年值
{FormattedWorkingDate(“ops”,values.year)}
{FormattedWorkingDate(“ops1”,values.year)}
{FormattedWorkingDate(“oms”,values.year)}
)}
/>
);
你是要在其他对象中手动设置日期和月份,还是真的能从某处获取它们?你能给我们一些伪代码来概括你想做什么吗?@RichieBendall我更新了我的问题,请参见上文。@ErtanHasani我更新了我的问题,请参见上文。您可以从每个数组中的日期
中获取日期和月份。例如这里的date:'01.07'
,1
是天,7
是月(七月)。非常感谢!是的,格式总是一样的,所以我把它放在方法中。唯一的问题是您丢失了getWorkingDay()
函数,因此我以返回格式(新日期(getWorkingDay(duedate)),“dd.MM.yyyy”)添加了它代码>