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

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

Choose a reason for hiding this comment

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

The assumption that the last element of fromFormat is a separator is incorrect. Separators should not be included in the format arrays. You should define separators separately or assume a default one.

const parts = date.split(separator);

const obj = {};

for (let i = 0; i < fromFormat.length - 1; i++) {
obj[fromFormat[i]] = parts[i];
}

if (toFormat.includes('YY') && fromFormat.includes('YYYY')) {
obj['YY'] = obj['YYYY'].slice(-2);
}
Comment on lines +20 to +22

Choose a reason for hiding this comment

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

The logic here assumes that the separator is part of the fromFormat, which is incorrect. The separator should be handled separately from the format array.


if (toFormat.includes('YYYY') && fromFormat.includes('YY')) {
const yy = Number(obj['YY']);

if (yy < 30) {
obj['YYYY'] = yy === 0 ? `2000` : `20${yy}`;
} else {
obj['YYYY'] = `19${yy}`;
}
}
Comment on lines +24 to +32

Choose a reason for hiding this comment

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

Similarly, the logic here assumes that the separator is part of the toFormat, which is incorrect. The separator should be handled separately from the format array.


const newDate = [];

for (let i = 0; i < toFormat.length - 1; i++) {
newDate.push(obj[toFormat[i]]);
}
Comment on lines +36 to +38

Choose a reason for hiding this comment

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

The loop should iterate over the entire toFormat array, not excluding the last element, as the separator should not be part of the format array.


const newSeparator = toFormat[toFormat.length - 1];

Choose a reason for hiding this comment

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

The assumption that the last element of toFormat is a separator is incorrect. Separators should not be included in the format arrays. You should define separators separately or assume a default one.


return newDate.join(newSeparator);
}

module.exports = formatDate;