-
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 #2218
base: master
Are you sure you want to change the base?
Solution #2218
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,38 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
const dateParts = {}; | ||
const separator = fromFormat.find( | ||
(part) => | ||
part !== 'DD' && part !== 'MM' && part !== 'YYYY' && part !== 'YY', | ||
); | ||
const splitDate = date.split(separator); | ||
|
||
for (let i = 0; i < fromFormat.length; i++) { | ||
if (fromFormat[i] === 'YY') { | ||
const year = splitDate[i]; | ||
|
||
dateParts['YYYY'] = | ||
year.length === 2 ? (+year < 30 ? `20${year}` : `19${year}`) : year; | ||
dateParts['YY'] = year.length === 2 ? year : year.slice(-2); | ||
} else { | ||
dateParts[fromFormat[i]] = splitDate[i]; | ||
} | ||
} | ||
|
||
if (toFormat.includes('YY') && !dateParts['YY']) { | ||
dateParts['YY'] = dateParts['YYYY'].slice(-2); | ||
} | ||
|
||
const newSeparator = toFormat.find( | ||
(part) => | ||
part !== 'DD' && part !== 'MM' && part !== 'YYYY' && part !== 'YY', | ||
); | ||
Comment on lines
+34
to
+37
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 determining the new separator is incorrect for the same reason as the |
||
|
||
return toFormat | ||
.slice(0, -1) | ||
.map((part) => dateParts[part]) | ||
.join(newSeparator); | ||
Comment on lines
+40
to
+42
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 |
||
} | ||
|
||
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 logic for determining the separator is incorrect. It assumes that the first non-date part is the separator, but this might not be the case if multiple separators exist. Consider using a regular expression to extract the separator from the date string.