-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
base: master
Are you sure you want to change the base?
Solution #2220
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return result; | ||
} | ||
|
||
module.exports = formatDate; |
There was a problem hiding this comment.
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.