-
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 #2170
base: master
Are you sure you want to change the base?
Solution #2170
Changes from 1 commit
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,29 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
const separator = fromFormat.pop(); | ||
const parts = date.split(separator); | ||
|
||
const dateParts = {}; | ||
|
||
fromFormat.forEach((part, index) => { | ||
dateParts[part] = parts[index]; | ||
}); | ||
|
||
if (toFormat.includes('YY') && dateParts['YYYY']) { | ||
dateParts['YY'] = dateParts['YYYY'].slice(-2); | ||
} | ||
|
||
if (toFormat.includes('YYYY') && dateParts['YY']) { | ||
dateParts['YYYY'] = | ||
dateParts['YY'] < '30' ? '20' + dateParts['YY'] : '19' + dateParts['YY']; | ||
Comment on lines
+28
to
+29
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 century for 'YY' might not be accurate for all cases. It assumes that any 'YY' less than '30' belongs to the 21st century, which might not be correct depending on the context. Ensure this logic aligns with the expected behavior. |
||
} | ||
|
||
const newSeparator = toFormat.pop(); | ||
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. Similar to the issue with |
||
|
||
const newDate = toFormat.map((part) => dateParts[part]).join(newSeparator); | ||
|
||
return newDate; | ||
} | ||
|
||
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 use of
pop()
onfromFormat
modifies the original array, which might not be intended iffromFormat
is used elsewhere. Consider usingslice()
orspread operator
to avoid mutating the input array.