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 #2199

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
47 changes: 46 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,52 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const partsDate = date.split(fromFormat[3]);

Choose a reason for hiding this comment

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

The use of fromFormat[3] to split the date assumes that the separator is always at index 3. This might not be correct if the format array has a different length. Consider dynamically determining the separator based on the format array.

let day = '';
let month = '';
let year = '';

for (let i = 0; i <= 3; i++) {

Choose a reason for hiding this comment

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

The loop condition i <= 3 assumes that the fromFormat array always has 4 elements. This might not be the case. Consider using fromFormat.length to make the loop more flexible.

if (fromFormat[i] === 'DD') {
day = partsDate[i];
}

if (fromFormat[i] === 'MM') {
month = partsDate[i];
}

if (fromFormat[i] === 'YY' || fromFormat[i] === 'YYYY') {
year = partsDate[i];
}
}

const newPartsDate = [];

for (let i = 0; i <= 3; i++) {

Choose a reason for hiding this comment

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

Similar to the previous loop, the condition i <= 3 assumes a fixed length for the toFormat array. Use toFormat.length to ensure the loop adapts to different format lengths.

if (toFormat[i] === 'DD') {
newPartsDate[i] = day;
}

if (toFormat[i] === 'MM') {
newPartsDate[i] = month;
}

if (toFormat[i] === 'YY') {
newPartsDate[i] = year.length === 2 ? year : year.slice(2);
}

if (toFormat[i] === 'YYYY') {
if (year.length === 4) {
newPartsDate[i] = year;
} else if (Number(year) < 30) {
newPartsDate[i] = '20' + year;
} else {
newPartsDate[i] = '19' + year;
}
}
}

return newPartsDate.join(toFormat[3]);
}

module.exports = formatDate;
Loading