-
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
add solution #2195
base: master
Are you sure you want to change the base?
add solution #2195
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,88 @@ | |
* @returns {string} | ||
*/ | ||
function formatDate(date, fromFormat, toFormat) { | ||
// write code here | ||
let result = ''; | ||
const oldDate = date; | ||
const newFormat = toFormat; | ||
let array = []; | ||
let year = ''; | ||
let yearBig = ''; | ||
const newArray = []; | ||
let indexYear = 0; | ||
let indexYearbig = 0; | ||
|
||
const [, , , oldSeparator] = fromFormat; | ||
const [, , , newSeparator] = newFormat; | ||
|
||
array = oldDate.split(oldSeparator); | ||
|
||
if (newFormat.includes('YYYY') && fromFormat.includes('YYYY')) { | ||
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. Ensure that the alignment of formats is checked before proceeding with transformations. The assumption that 'YYYY' is present in both |
||
for (const n of newFormat) { | ||
const searchInd = fromFormat.indexOf(n); | ||
|
||
newArray.push(array[searchInd]); | ||
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 |
||
} | ||
newArray.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. The use of |
||
result = newArray.join(newSeparator); | ||
|
||
return result; | ||
} | ||
Comment on lines
+26
to
+36
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 both 'YYYY' are present in |
||
|
||
if (newFormat.includes('YY')) { | ||
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 handling 'YY' is not flexible and might not be suitable for all use cases. Consider making this logic more adaptable. |
||
for (const n of array) { | ||
if (n.length > 3) { | ||
indexYear = array.indexOf(n); | ||
year = n.slice(2); | ||
} | ||
} | ||
array[indexYear] = year; | ||
|
||
const newFormatYY = newFormat; | ||
const newYY = newFormatYY.indexOf('YY'); | ||
|
||
newFormatYY[newYY] = 'YYYY'; | ||
|
||
for (const n of newFormatYY) { | ||
const searchInd = fromFormat.indexOf(n); | ||
|
||
newArray.push(array[searchInd]); | ||
} | ||
newArray.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. As with the previous comment, ensure that |
||
|
||
result = newArray.join(newSeparator); | ||
|
||
return result; | ||
} | ||
|
||
if (newFormat.includes('YYYY')) { | ||
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 handling of 'YYYY' when 'YY' is present in |
||
indexYearbig = fromFormat.indexOf('YY'); | ||
|
||
if (parseInt(array[indexYearbig]) >= 30) { | ||
yearBig = '19' + array[indexYearbig]; | ||
} | ||
|
||
if (parseInt(array[indexYearbig]) < 30) { | ||
yearBig = '20' + array[indexYearbig]; | ||
} | ||
|
||
array[indexYearbig] = yearBig; | ||
|
||
const newFormatYYYY = newFormat; | ||
const newY = newFormatYYYY.indexOf('YYYY'); | ||
|
||
newFormatYYYY[newY] = 'YY'; | ||
|
||
for (const n of newFormatYYYY) { | ||
const searchInd = fromFormat.indexOf(n); | ||
|
||
newArray.push(array[searchInd]); | ||
} | ||
newArray.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. Again, verify that |
||
|
||
result = newArray.join(newSeparator); | ||
|
||
return result; | ||
} | ||
} | ||
|
||
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.
Directly modifying
newFormat
can lead to unexpected behavior if the same format is reused elsewhere. Consider working with a copy ofnewFormat
to avoid side effects.