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

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
26 changes: 25 additions & 1 deletion src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,31 @@
* @returns {string}
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const [fromParts, separator] = [fromFormat.slice(0, -1), fromFormat[3]];
const [toParts, separatorNew] = [toFormat.slice(0, -1), toFormat[3]];
Comment on lines +11 to +12

Choose a reason for hiding this comment

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

The assumption that the separator is at index 3 in both fromFormat and toFormat might not always be correct. If the format strings are not exactly 4 characters long, this will lead to incorrect behavior. Consider using a different approach to determine the separator, such as checking for non-alphanumeric characters in the format string.

const dateParts = date.split(separator);
const dateFrom = {};
const dateTo = [];

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

if (dateFrom.YYYY) {
dateFrom.YY = dateFrom.YYYY.slice(-2);
} else if (dateFrom.YY) {
if (dateFrom.YY < 30) {
dateFrom.YYYY = `20${dateFrom.YY}`;
} else {
dateFrom.YYYY = `19${dateFrom.YY}`;
}
}

for (let i = 0; i < toParts.length; i++) {
dateTo.push(dateFrom[toParts[i]]);
}

return dateTo.join(separatorNew);
}

module.exports = formatDate;
Loading