-
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 1 #1935
base: master
Are you sure you want to change the base?
solution 1 #1935
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,50 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
const dateParts = date.split(fromFormat[3]); | ||
let year; | ||
let month; | ||
let day; | ||
const formattedDate = []; | ||
|
||
for (let i = 0; i < fromFormat.length; i++) { | ||
if (fromFormat[i] === 'YYYY') { | ||
year = dateParts[i]; | ||
} | ||
|
||
if (fromFormat[i] === 'YY') { | ||
year = dateParts[i] < 30 ? `20${dateParts[i]}` : `19${dateParts[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 condition |
||
} | ||
|
||
if (fromFormat[i] === 'MM') { | ||
month = dateParts[i]; | ||
} | ||
|
||
if (fromFormat[i] === 'DD') { | ||
day = dateParts[i]; | ||
} | ||
} | ||
|
||
for (const format of toFormat) { | ||
switch (format) { | ||
case 'YYYY': | ||
formattedDate.push(year); | ||
break; | ||
|
||
case 'YY': | ||
formattedDate.push(year.slice(-2)); | ||
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. There is an implicit assumption that |
||
break; | ||
|
||
case 'MM': | ||
formattedDate.push(month); | ||
break; | ||
|
||
case 'DD': | ||
formattedDate.push(day); | ||
} | ||
} | ||
|
||
return formattedDate.join(toFormat[3]); | ||
Comment on lines
10
to
+54
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 implementation of the
Comment on lines
10
to
+54
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 implementation of 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.
It's assumed that the separator is always the last element in the
fromFormat
array. While this works for the given examples, it might be safer to explicitly search for the separator in thefromFormat
array to avoid potential issues if the array structure changes.