-
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 #2192
base: master
Are you sure you want to change the base?
solution #2192
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,33 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
const dateParts = {}; | ||
const newDateParts = []; | ||
const split = fromFormat.pop(); | ||
const newSplit = 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. Using |
||
const cutDate = date.split(split); | ||
|
||
for (let i = 0; i < fromFormat.length; i++) { | ||
dateParts[fromFormat[i]] = cutDate[i]; | ||
} | ||
|
||
for (const part of toFormat) { | ||
if (part === 'YY' && dateParts['YYYY']) { | ||
newDateParts.push(dateParts['YYYY'].slice(-2)); | ||
} else if (part === 'YYYY' && dateParts['YY']) { | ||
const year = parseInt(dateParts['YY'], 10); | ||
|
||
if (year < 30) { | ||
newDateParts.push('20' + (year < 10 ? '0' + year : year)); | ||
} else { | ||
newDateParts.push('19' + year); | ||
Comment on lines
+27
to
+30
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 based on a two-digit year assumes that years less than 30 belong to the 2000s and others to the 1900s. This assumption may not be suitable for all use cases. Consider making this logic configurable or documenting this behavior clearly. |
||
} | ||
} else { | ||
newDateParts.push(dateParts[part]); | ||
} | ||
} | ||
|
||
return newDateParts.join(newSplit); | ||
} | ||
|
||
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.
Using
pop()
onfromFormat
modifies the array destructively, which may lead to unexpected behavior iffromFormat
is reused elsewhere. Consider usingslice(-1)[0]
to get the last element without modifying the array.