Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #2219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,63 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const [, , , fromSeparator] = fromFormat;
const [, , , toSeparator] = toFormat;
const resultDate = date.split(`${fromSeparator}`);
const day = resultDate[fromFormat.indexOf('DD')];
const month = resultDate[fromFormat.indexOf('MM')];
let year;
let indexOfYear;

if (fromFormat.includes('YY')) {
const yearFormat = 'YY';

indexOfYear = fromFormat.indexOf(yearFormat);
year = resultDate[indexOfYear];

if (toFormat.includes('YYYY')) {
resultDate[indexOfYear] = yearChanger(resultDate[indexOfYear]);
}

if (fromFormat.indexOf('DD') !== toFormat.indexOf('DD')) {
resultDate[toFormat.indexOf('DD')] = day;
resultDate[toFormat.indexOf('MM')] = month;
resultDate[toFormat.includes(yearFormat)] = year;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line resultDate[toFormat.includes(yearFormat)] = year; is incorrect. The includes method returns a boolean, not an index. You should use toFormat.indexOf(yearFormat) to get the correct index for the year.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line resultDate[toFormat.includes(yearFormat)] = year; is incorrect. The includes method returns a boolean, so using it as an index will not work as intended. You should use toFormat.indexOf(yearFormat) to get the correct index for the year in the toFormat array.

}
}

if (fromFormat.includes('YYYY')) {
const yearFormat = 'YYYY';

indexOfYear = fromFormat.indexOf(yearFormat);
year = resultDate[indexOfYear];

if (toFormat.includes('YY')) {
resultDate[indexOfYear] = yearChanger(resultDate[indexOfYear]);
}

if (fromFormat.indexOf('DD') !== toFormat.indexOf('DD')) {
resultDate[toFormat.indexOf('DD')] = day;
resultDate[toFormat.indexOf('MM')] = month;
resultDate[toFormat.indexOf(yearFormat)] = year;
}
}

return resultDate.join(`${toSeparator}`);
}

function yearChanger(year) {
if (year.length === 4) {
return `${year[2]}${year[3]}`;
}

if (year.length === 2) {
if (year < 30) {
return `20${year}`;
} else {
return `19${year}`;
}
}
}

module.exports = formatDate;
Loading