-
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 #2199
base: master
Are you sure you want to change the base?
Solution #2199
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,52 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
const partsDate = date.split(fromFormat[3]); | ||
let day = ''; | ||
let month = ''; | ||
let year = ''; | ||
|
||
for (let i = 0; i <= 3; i++) { | ||
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 condition |
||
if (fromFormat[i] === 'DD') { | ||
day = partsDate[i]; | ||
} | ||
|
||
if (fromFormat[i] === 'MM') { | ||
month = partsDate[i]; | ||
} | ||
|
||
if (fromFormat[i] === 'YY' || fromFormat[i] === 'YYYY') { | ||
year = partsDate[i]; | ||
} | ||
} | ||
|
||
const newPartsDate = []; | ||
|
||
for (let i = 0; i <= 3; i++) { | ||
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 previous loop, the condition |
||
if (toFormat[i] === 'DD') { | ||
newPartsDate[i] = day; | ||
} | ||
|
||
if (toFormat[i] === 'MM') { | ||
newPartsDate[i] = month; | ||
} | ||
|
||
if (toFormat[i] === 'YY') { | ||
newPartsDate[i] = year.length === 2 ? year : year.slice(2); | ||
} | ||
|
||
if (toFormat[i] === 'YYYY') { | ||
if (year.length === 4) { | ||
newPartsDate[i] = year; | ||
} else if (Number(year) < 30) { | ||
newPartsDate[i] = '20' + year; | ||
} else { | ||
newPartsDate[i] = '19' + year; | ||
} | ||
} | ||
} | ||
|
||
return newPartsDate.join(toFormat[3]); | ||
} | ||
|
||
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
fromFormat[3]
to split the date assumes that the separator is always at index 3. This might not be correct if the format array has a different length. Consider dynamically determining the separator based on the format array.