-
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
done #2211
base: master
Are you sure you want to change the base?
done #2211
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,56 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
const separator = fromFormat[fromFormat.length - 1]; | ||
const splittedDate = date.split(separator); | ||
|
||
const result = []; | ||
|
||
for (let i = 0; i < toFormat.length - 1; i++) { | ||
const firstLetter = toFormat[i][0]; | ||
|
||
for (const el of fromFormat) { | ||
if (el.includes(firstLetter)) { | ||
const index = fromFormat.indexOf(el); | ||
|
||
result[i] = splittedDate[index]; | ||
} | ||
} | ||
} | ||
|
||
for (const el of toFormat) { | ||
if (el.includes('Y') && el.length === 2) { | ||
const index = toFormat.indexOf(el); | ||
|
||
if (result[index].length === 2) { | ||
return result.join(toFormat[toFormat.length - 1]); | ||
} | ||
|
||
if (result[index].length === 4) { | ||
result[index] = result[index].slice(-2); | ||
|
||
return result.join(toFormat[toFormat.length - 1]); | ||
Comment on lines
+36
to
+39
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. Similarly, returning here when the year is in 'YYYY' format and is already four digits long may not be correct. Ensure that all parts of the date are formatted before returning. |
||
} | ||
} | ||
|
||
if (el.includes('Y') && el.length === 4) { | ||
const index = toFormat.indexOf(el); | ||
|
||
if (result[index].length === 2) { | ||
if (+result[index] < 30) { | ||
result[index] = 20 + result[index]; | ||
|
||
return result.join(toFormat[toFormat.length - 1]); | ||
} else { | ||
result[index] = 19 + result[index]; | ||
|
||
return result.join(toFormat[toFormat.length - 1]); | ||
Comment on lines
+46
to
+54
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 two-digit years to four-digit years is correct, but returning immediately after updating the year might skip formatting other parts of the date. Consider updating the |
||
} | ||
} | ||
} | ||
} | ||
|
||
return result.join(toFormat[toFormat.length - 1]); | ||
} | ||
|
||
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 function returns prematurely if the year is in 'YY' format and is already two digits long. This might not be the intended behavior, as it skips formatting other parts of the date. Consider removing the return statement here and allowing the loop to continue.