-
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 #2295
base: master
Are you sure you want to change the base?
Solution #2295
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 separator = fromFormat[fromFormat.length - 1]; | ||
const parts = date.split(separator); | ||
|
||
const obj = {}; | ||
|
||
for (let i = 0; i < fromFormat.length - 1; i++) { | ||
obj[fromFormat[i]] = parts[i]; | ||
} | ||
|
||
if (toFormat.includes('YY') && fromFormat.includes('YYYY')) { | ||
obj['YY'] = obj['YYYY'].slice(-2); | ||
} | ||
Comment on lines
+20
to
+22
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 here assumes that the separator is part of the |
||
|
||
if (toFormat.includes('YYYY') && fromFormat.includes('YY')) { | ||
const yy = Number(obj['YY']); | ||
|
||
if (yy < 30) { | ||
obj['YYYY'] = yy === 0 ? `2000` : `20${yy}`; | ||
} else { | ||
obj['YYYY'] = `19${yy}`; | ||
} | ||
} | ||
Comment on lines
+24
to
+32
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, the logic here assumes that the separator is part of the |
||
|
||
const newDate = []; | ||
|
||
for (let i = 0; i < toFormat.length - 1; i++) { | ||
newDate.push(obj[toFormat[i]]); | ||
} | ||
Comment on lines
+36
to
+38
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 loop should iterate over the entire |
||
|
||
const newSeparator = toFormat[toFormat.length - 1]; | ||
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 assumption that the last element of |
||
|
||
return newDate.join(newSeparator); | ||
} | ||
|
||
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 assumption that the last element of
fromFormat
is a separator is incorrect. Separators should not be included in the format arrays. You should define separators separately or assume a default one.