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

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
39 changes: 38 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,44 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const [fromPart1, fromPart2, fromPart3, separatorFrom] = fromFormat; //
const [toPart1, toPart2, toPart3, separatorTo] = toFormat; //
const [part1, part2, part3] = date.split(separatorFrom);
let result = date.split(separatorFrom).join(separatorTo);

Choose a reason for hiding this comment

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

The initial assignment of result is not effectively utilized. The logic for formatting the date should be based on the conditions, and this initial assignment may lead to incorrect results if not properly updated.

let newPart;

if (fromPart1 === toPart3) {
result = date.split(separatorFrom).reverse().join(separatorTo);
}

if (fromPart1 === toPart2 && fromPart2 === toPart3) {
result = [part3, part1, part2].join(separatorTo);
}

if (fromPart3 === 'YYYY' && toPart3 === 'YY') {
newPart = part3.slice(-2);

result = [part1, part2, newPart].join(separatorTo);
}

if (fromPart3 === 'YYYY' && toPart3 === 'YY') {
newPart = part3.slice(-2);
result = [part1, part2, newPart].join(separatorTo);
}
Comment on lines +25 to +34

Choose a reason for hiding this comment

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

There are duplicate conditions for converting 'YYYY' to 'YY'. Lines 25-29 and 31-34 perform the same operation. You should remove one of these duplicate blocks to avoid redundancy.


if (fromPart1 === 'YY' && toPart1 === 'YYYY') {
if (Number(part1) < 30) {
result = '20' + result;
}
}

if (fromPart1 === 'YY' && toPart1 === 'YYYY') {
if (part1 >= 30) {
result = '19' + result;
}
}
Comment on lines +36 to +46

Choose a reason for hiding this comment

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

The logic for converting 'YY' to 'YYYY' is flawed. The current implementation incorrectly appends '20' or '19' to the entire result string, which could lead to incorrect formatting. You should prepend '20' or '19' only to the part1 and then reconstruct the date.


return result;
}

module.exports = formatDate;
Loading