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 #2192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
28 changes: 27 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,33 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const dateParts = {};
const newDateParts = [];
const split = fromFormat.pop();

Choose a reason for hiding this comment

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

Using pop() on fromFormat modifies the array destructively, which may lead to unexpected behavior if fromFormat is reused elsewhere. Consider using slice(-1)[0] to get the last element without modifying the array.

const newSplit = toFormat.pop();

Choose a reason for hiding this comment

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

Using pop() on toFormat modifies the array destructively, which may lead to unexpected behavior if toFormat is reused elsewhere. Consider using slice(-1)[0] to get the last element without modifying the array.

const cutDate = date.split(split);

for (let i = 0; i < fromFormat.length; i++) {
dateParts[fromFormat[i]] = cutDate[i];
}

for (const part of toFormat) {
if (part === 'YY' && dateParts['YYYY']) {
newDateParts.push(dateParts['YYYY'].slice(-2));
} else if (part === 'YYYY' && dateParts['YY']) {
const year = parseInt(dateParts['YY'], 10);

if (year < 30) {
newDateParts.push('20' + (year < 10 ? '0' + year : year));
} else {
newDateParts.push('19' + year);
Comment on lines +27 to +30

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 based on a two-digit year assumes that years less than 30 belong to the 2000s and others to the 1900s. This assumption may not be suitable for all use cases. Consider making this logic configurable or documenting this behavior clearly.

}
} else {
newDateParts.push(dateParts[part]);
}
}

return newDateParts.join(newSplit);
}

module.exports = formatDate;
Loading