Skip to content
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 #2170

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,29 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const separator = fromFormat.pop();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of pop() on fromFormat modifies the original array, which might not be intended if fromFormat is used elsewhere. Consider using slice() or spread operator to avoid mutating the input array.

const parts = date.split(separator);

const dateParts = {};

fromFormat.forEach((part, index) => {
dateParts[part] = parts[index];
});

if (toFormat.includes('YY') && dateParts['YYYY']) {
dateParts['YY'] = dateParts['YYYY'].slice(-2);
}

if (toFormat.includes('YYYY') && dateParts['YY']) {
dateParts['YYYY'] =
dateParts['YY'] < '30' ? '20' + dateParts['YY'] : '19' + dateParts['YY'];
Comment on lines +28 to +29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining the century for 'YY' might not be accurate for all cases. It assumes that any 'YY' less than '30' belongs to the 21st century, which might not be correct depending on the context. Ensure this logic aligns with the expected behavior.

}

const newSeparator = toFormat.pop();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the issue with fromFormat, using pop() on toFormat modifies the original array. Consider using slice() or spread operator to avoid mutating the input array.


const newDate = toFormat.map((part) => dateParts[part]).join(newSeparator);

return newDate;
}

module.exports = formatDate;
Loading