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

Solution #2159

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
64 changes: 63 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,69 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const separator = fromFormat[3];

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the fromFormat array, which may not be correct for all date formats. Consider dynamically determining the separator based on the actual format provided.

const dateArray = date.split(separator);
let day = '';
let month = '';
let year = '';

if (fromFormat[0] === 'YYYY' || fromFormat[0] === 'YY') {
year = dateArray[0];
month = dateArray[1];
day = dateArray[2];

Choose a reason for hiding this comment

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

Now these values ​​are duplicated in many places, store them in a variable and use the variable everywhere
image

Copy link
Author

Choose a reason for hiding this comment

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

ПОдскажи как это сделать?

} else if (fromFormat[1] === 'YYYY') {
year = dateArray[1];
month = dateArray[0];
day = dateArray[2];
} else {
year = dateArray[2];
month = dateArray[1];
day = dateArray[0];
}

if (toFormat[0] === 'YYYY' && year.length === 4) {
return year + toFormat[3] + month + toFormat[3] + day;

Choose a reason for hiding this comment

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

The code assumes that the separator is always at index 3 of the toFormat array. This assumption may not hold true if the format does not include a separator at that index. Consider revising the logic to dynamically determine the separator based on the format.

Comment on lines +25 to +26

Choose a reason for hiding this comment

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

The code assumes the separator is at index 3 of the toFormat array. This assumption may not hold true for all date formats. Consider revising the logic to dynamically determine the separator.

}

if (toFormat[0] === 'YY' && year.length === 4) {
return year.slice(2) + toFormat[3] + month + toFormat[3] + day;

Choose a reason for hiding this comment

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

Similar to the previous comment, the assumption about the separator being at index 3 of the toFormat array may not be valid. Ensure that the separator is correctly identified based on the format.

Comment on lines +29 to +30

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array. This assumption may not hold true for all date formats. Consider revising the logic to dynamically determine the separator.

}

if (toFormat[0] === 'YY' && year.length === 2) {
return year + toFormat[3] + month + toFormat[3] + day;

Choose a reason for hiding this comment

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

Again, the assumption about the separator at index 3 of the toFormat array may not be correct. Adjust the logic to handle different formats appropriately.

Comment on lines +33 to +34

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array. This assumption may not hold true for all date formats. Consider revising the logic to dynamically determine the separator.

}

if (toFormat[0] === 'YYYY' && year.length === 2) {
if (+year >= 30) {
year = '19' + year;
} else {
year = '20' + year;
}

return year + toFormat[3] + month + toFormat[3] + day;

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array, which may not always be the case. Consider revising the logic to correctly identify the separator.

Comment on lines +37 to +44

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array. This assumption may not hold true for all date formats. Consider revising the logic to dynamically determine the separator.

}

if (toFormat[2] === 'YYYY' && year.length === 4) {
return day + toFormat[3] + month + toFormat[3] + year;

Choose a reason for hiding this comment

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

The code assumes the separator is at index 3 of the toFormat array. This may not be accurate for all formats. Adjust the logic to handle different formats correctly.

Comment on lines +47 to +48

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array. This assumption may not hold true for all date formats. Consider revising the logic to dynamically determine the separator.

}

if (toFormat[2] === 'YY' && year.length === 4) {
return day + toFormat[3] + month + toFormat[3] + year.slice(2);

Choose a reason for hiding this comment

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

The assumption about the separator being at index 3 of the toFormat array may not be valid. Ensure the separator is correctly identified based on the format.

Comment on lines +51 to +52

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array. This assumption may not hold true for all date formats. Consider revising the logic to dynamically determine the separator.

}

if (toFormat[2] === 'YY' && year.length === 2) {
return day + toFormat[3] + month + toFormat[3] + year;

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array, which may not always be the case. Consider revising the logic to correctly identify the separator.

Comment on lines +55 to +56

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array. This assumption may not hold true for all date formats. Consider revising the logic to dynamically determine the separator.

}

if (toFormat[2] === 'YYYY' && year.length === 2) {
if (+year >= 30) {
year = '19' + year;
} else {
year = '20' + year;
}

return day + toFormat[3] + month + toFormat[3] + year;

Choose a reason for hiding this comment

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

The code assumes the separator is at index 3 of the toFormat array. This may not be accurate for all formats. Adjust the logic to handle different formats correctly.

Comment on lines +59 to +66

Choose a reason for hiding this comment

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

The separator is assumed to be at index 3 of the toFormat array. This assumption may not hold true for all date formats. Consider revising the logic to dynamically determine the separator.

}
}

module.exports = formatDate;
Loading