I have some arrays defined like this:
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' }
];
I need to parse due dates in three different places through a function getWorkingDay(). The due date consists of a date from an array's object and a year value, which is passed through react-final-form values. If the object has nextYear: 'true', the year should be incremented by one.
I can get the due date for a single array (for example, ops) like this:
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);
And then parse it through getWorkingDay() and format():
<div>{format(new Date(getWorkingDay(duedate)), 'dd.MM.yyyy')}</div>
My questions are:
- How can I create a function to work with all arrays, in which I can pass the name of the array as a parameter?
- How can I pass
yearvalue as a second parameter fromreact-final-formvalues to this function, instead of hard codingconst yearFull = '2019';? - How can I check
nextYear: 'true'from this function to increment the year by one? (duedate.setFullYearshould be the year or the year + 1, whennextYearis true) - Is it better to make
dateas an object or an array instead of a string to work with this function?
datein each array. For example heredate: '01.07',1is day and7is month (July).